Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  120] [ 2]  / answers: 1 / hits: 14010  / 10 Years ago, wed, october 15, 2014, 12:00:00

I've been developing a web application Dashboard and I was wondering how to detect that is midnight in order to reset some arrays that contains datas from the previous day using jquery or momentjs.


More From » jquery

 Answers
3

Use moment().format(h:mm:ss) that returns time in a h:mm:ss format.



var midnight = 0:00:00;
var now = null;

setInterval(function () {
now = moment().format(H:mm:ss);
if (now === midnight) {
alert(Hi);
}
$(#time).text(now);
}, 1000);


JSFIDDLE



A better way would be to compute the seconds until midnight. This is very simple and human readable using MomentJS:



// returns the number of seconds until next midnight
moment(24:00:00, hh:mm:ss).diff(moment(), 'seconds');


So, just do:



setTimeout(
midnightTask,
moment(24:00:00, hh:mm:ss).diff(moment(), 'seconds')
);

function midnightTask() {
/* do something */
}


JSFIDDLE


[#41881] Tuesday, October 14, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tayaw

Total Points: 749
Total Questions: 88
Total Answers: 86

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
tayaw questions
;