I am using react native push notification.
But when i am trying to push local schedule, notification not show as time expected. It always showed when i reload the app (same behaviour like PushNotification.localNotification
how to set date for parameter inside PushNotification.localNotificationSchedule ? i am confuse to set value there. I already using timestamp but looks like the value is different.
1578909865373 => using date.now
1578907260 => i convert from string date to timestamp
my code :
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum/1000;
}
const dated= toTimestamp('01/13/2020 16:21:00');
PushNotification.localNotificationSchedule({
//... You can use all the options from localNotifications
message: "testing notification", // (required)
date: date: new Date(dated) // in 60 secs
// date: new Date("2020/01/13")
});
}
please help how to insert or convert right value for date.
Thank you
Turn out i dont have to divide by 1000
timestamp is milliseconds.
so right function is
function toTimestamp(strDate){
var datum = Date.parse(strDate);
return datum;
}
Related
I am currently using the react-native-push-notification library to schedule and receive notifications in my React Native app. I am able to cancel a scheduled notification using the cancelLocalNotification method, but this only cancels the notification for 24 hours. I want to find a way to remove a specific notification forever, so it will not be rescheduled.
I have tried using the following code to cancel a notification by its ID, but it only cancels it for 24 hours:
const onCancelNotification = async (id: string) => {
// Get a list of all scheduled notifications
PushNotification.getScheduledLocalNotifications((notifications) => {
// Iterate through the list of notifications
notifications.forEach((notification) => {
// Check if the notification is the one you want to cancel
if (notification.id.indexOf(id) === 0) {
// Cancel the notification
PushNotification.cancelLocalNotification(notification.id);
}
});
});
};
I would greatly appreciate any help or suggestions on how to achieve this.
This code snippet demonstrates a workaround for removing a specific scheduled local notification in React Native. The function onRemoveNotification takes in an id parameter, which is used to identify the specific notification that needs to be removed.
It's important to note that there is no direct method for removing a specific scheduled local notification in React Native. This code provides a workaround that can be used, but it's important to be aware that it relies on scheduling a notification with an earlier date and setting the repeatType to undefined.
Please note that this code is not a perfect solution and could have side effects on the app.
const onRemoveNotification = async (id: string) => {
// Get a list of all scheduled notifications
PushNotification.getScheduledLocalNotifications((notifications) => {
// Iterate through the list of notifications
notifications.forEach((notification) => {
// Check if the notification is the one you want to cancel
if (notification.data.notificationId?.indexOf(id) === 0) {
// Create a new date one day earlier than the current scheduled date
const earlyDate = moment(notification.date).add(-1, "day").toDate();
// remove the notification
// schedule the notification with an earlier date and repeat type undefined
// this will effectively "remove" the notification
// since it will not be displayed
PushNotification.localNotificationSchedule({
id: notification.id,
title: notification.title,
message: notification.message,
repeatType: undefined,
date: earlyDate,
});
// cancel the previous scheduled notification
PushNotification.cancelLocalNotification(notification.id);
}
});
});
};
I'm implementing a system and I need to set the timezone for the device depending on the item selected from a list that I'm showing to the user.
This is the design the customer wants:
I managed to implement this design. The problem is how can I set the timezone in the system, using only the code from the list? (for example: "UTC-12:00")
I've been reading around and the solution proposed is to use Javascript moment library, but it doesn't allow me to change the timezone like this.
The code I tried for the Save button is this:
saveTimeZone() {
if ( this.newTimeZone ) {
moment( new Date() ).tz.setDefault( this.newTimeZone );
}
}
And the value newTimeZone is setted up when clicked the list item:
selectTimeZone( item ) {
if ( this.getTime !== item ) {
this.newTimeZone = item;
this.disableSafeBtn = false;
}
}
Does anyone have any workaround for this?
I want to convert the String to a date and compare it to the current one.
When I saw the results after coding Debug JS Remotely, it worked.
However, if Debug JS is stopped and then executed, the result will be changed to invalid Date.
How can I produce consistent results?
const checkNotice = async () => {
const noticeStart = new Date('2019-04-30 00:01:02');
const noticeEnd = new Date('2019-10-07 23:59:59');
const now = new Date();
if (now > noticeStart && now < noticeEnd) {
return {
NoticeStart: noticeStart,
NoticeEnd: noticeEnd,
};
}
return false;
};
When this code is in Debug mode
noticeStart and noticeEnd are replaced by Date.
However, if stop Debug mode, it will change to invalid Date and will always return false.
Everything else is the same.
Only the debug mode is different.
To compare two Date object, you can try doing
now.getTime() > noticeStart.getTime() && now.getTime() < noticeEnd.getTime()
Maybe this is because the behavior is different between the web browser JS interpreter and React Native JSCore. Have you looked at the issued on React-Native repository? ^^
I want to add 5 minutes to current time and store it to this.state.date.
However,
this.setState({
date: new Date() + new Date(0,0,0,0,0,5,0),
});
gives me some strange error, saying
`props.date.getTime is not a function. (In 'props.date.getTime()', 'props.date.getTime' is undefined)
DatePickerIOS_render
DatePickerIOS.ios.js:125
EDITED: According to this post I also tried below,
this.setState({
date: this.state.date.setMinutes((new Date()).getMinutes()+5)
});
but it still gives me the same error message. What is the correct way to add minutes to Date() in react-native?
Btw, I'm using datepickerios on another part of the app, and not sure why the error is related to DatePickerIOS.ios.js in the first place. The other part of the app which actually uses the DatePickerIOS works fine without error.
EDITED AGAIN:
Below is the full function. This function is called from a SegmentedControlIOS which has values ['5min from now', '15min from now', '30min from now', 'custom'].
_onTimeChange = (event) => {
switch (event.nativeEvent.selectedSegmentIndex) {
case 0:
// 5min
return this.setState({
date: this.state.date.setMinutes((new Date()).getMinutes()+5),
time: this.state.date.toLocaleTimeString()
});
case 1:
// 15min
return this.setState({
date: this.state.date.setMinutes((new Date()).getMinutes()+15),
time: this.state.date.toLocaleTimeString()
});
case 2:
// 30min
return this.setState({
date: this.state.date.setMinutes((new Date()).getMinutes()+30),
time: this.state.date.toLocaleTimeString()
});
case 3:
// show modal with datepicker (this one works fine)
return this.setModalVisible(true);
}
}
What happens: If I change selection from the segmented control once, a yellow warning bar with props error message is shown at the bottom of my simulator. If I change selection once again, then the red screen shows up with '_this5.state.date.setMinutes is not a function'.
So it looks like
date: this.state.date.setMinutes((new Date()).getMinutes()+5)
does add minutes, but when I do it twice it ends up in error.
Please help!
Try this:
var d = new Date(); // get current date
d.setHours(d.getHours(),d.getMinutes()+5,0,0);
EDIT
var d = new Date(); // get current date
d.setHours(d.getHours(),d.getMinutes()+5,0,0);
this.setState(date:d,time:d.toLocaleTimeString());
I am using Bootstrap time Picker control and store data in DB in string format like 6:30 PM. Now I have an issue when I edit the form and put that value in Bootstrap time Picker control, I got an issue, Any one can help in this regard.
String format is ="6:30 PM" and want to set that string in Bootstrap time Picker control.
EDIT:
I am filling in below format.
blockHoursDetail.FillBlockHours(blockHoursDetail.params.BlockHoursId).done(function (response) {
if (response.status != false) {
var blockHours_detail = JSON.parse(response.BlockHoursFill_JSON);
var self = $("#blockHoursDetail");
utility.bindMyJSON(true, blockHours_detail, false, self);
if (blockHours_detail.chkActive == 'True')
$("#blockHoursDetail #chkActive").attr("checked", true);
else
$("#blockHoursDetail #chkActive").attr("checked", false);
blockHoursDetail.ValidateBlockHours();
$('#frmBlockHoursDetail').data('serialize', $('#frmBlockHoursDetail').serialize());
}
else {
utility.DisplayMessages(response.Message, 3);
}
});
When you return data from the db you should show it like that.
<?php echo date("h:i A",strtotime($time_from_db)); ?>
and if you are facing some problem in initializing of time picker the the specific format then you can use this
$('.timepicker').timepicker('setTime', "6:30 PM");
hope it will help you