0

I'm new at notifications and as well as working with dates and time. I've tried finding things like mine but i can't find anything that changes the time and not just the date.

var eventTime = Event.date;
var _24_hours_before_event = new Date(eventTime - 86400000);

cordova.plugins.notification.local.schedule({
    id: Math.floor(Math.random() * Number.MAX_VALUE) + 1,
    title: Event.title + "is in 24 hours",
    text: Event.description,
    at: _24_hours_before_event,
    data: {
        eventId: Event._id,
        time: 24
    }
});

As you can see I'm trying to send a user a notification, warning them that an event will be in 24 hours. Now the way it knows when to send the notification is the user puts in a time, saved in Event.date. Then, given that time, it will subtract 24 hours (That's the part i'm stuck on). Event.date would look something like this 2016-01-09T14:00:00.000Z. I tried doing something like this Date(eventTime - 86400000),but that didn't work. What i need help is how I can make this 2016-01-09T14:00:00.000Z go back a full day. Which should look like this 2016-01-08T14:00:00.000Z. Also, just to be clear, if the time is 5:00am Jan 6 2013 then i want the notification to show up at 5:00am Jan 5 2013. Thanks for your help.

Solution

I found out that 2016-01-08T14:00:00.000Z or Event.date is a string and not an integer. The way I made it an integer was doing new Date(Event.date) like so:

var eventTime = new Date(Event.date);
var _24_hours_before_event = new Date(eventTime.getTime() - 86400000);

cordova.plugins.notification.local.schedule({
   id: Math.floor(Math.random() * Number.MAX_VALUE) + 1,
   title: Event.title + "is in 24 hours",
   text: Event.description,
   at: _24_hours_before_event,
   data: {
       eventId: Event._id,
       time: 24
   }
});

I also added .getTime() so 86400000 can get subtracted from the date given.

1 Answers1

0

If you would use Moment you could set your _24_hours_before_event variable to the following:

moment(eventTime).subtract(1, 'days').toDate();

If you do a lot of date-handling it might be worth to consider.

You can check out the full documentation here.

E. Sundin
  • 4,103
  • 21
  • 30