I'm building a chrome extension now.
I'd like to set an alarm to be fired at 23:59:59 everyday, so I can reset some saved settings at the end of day.
I wrote this code below but, I'm not used to using Date Object. And I'm worried if this code works fine and don't mess up everything.
Especially, I'm worried about the code for setting up an alarm again for the next day.
var day = now.getDate() + 1;
To get the date of the following day, I just add 1 to the returned value of "now.getDate()". But, I wonder if the returned value of "now.getDate()" is the end of the month and because of that, adding 1 ends up getting the date which doesn't exist.
Please take a look at my code and tell me if this works fine or not.
Thank you in advance!!
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate();
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for today at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
// when alarm fires, do the following.
chrome.alarms.onAlarm.addListener(function() {
//clear some saved settings at the end of day.
//After that, set an alarm again for tomorrow at 23:59:59.
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth();
var day = now.getDate() + 1;
var timestamp = Number(new Date(year, month, day, 23, 59, 59, 0));
//set an alarm for tomorrow at 23:59:59.
chrome.alarms.create('resetSpentTime', {
when: timestamp
});
})