Determining minutes to midnight

How would you determine how many minutes to midnight this day using javascript?

+4
source share
3 answers
function minutesUntilMidnight() { var midnight = new Date(); midnight.setHours( 24 ); midnight.setMinutes( 0 ); midnight.setSeconds( 0 ); midnight.setMilliseconds( 0 ); return ( midnight.getTime() - new Date().getTime() ) / 1000 / 60; } 
+6
source

May be:

 function minsToMidnight() { var now = new Date(); var then = new Date(now); then.setHours(24, 0, 0, 0); return (then - now) / 6e4; } console.log(minsToMidnight()); 

or

 function minsToMidnight() { var msd = 8.64e7; var now = new Date(); return (msd - (now - now.getTimezoneOffset() * 6e4) % msd) / 6e4; } console.log(minsToMidnight()) 

and there are:

 function minsToMidnight(){ var d = new Date(); return (-d + d.setHours(24,0,0,0))/6e4; } console.log(minsToMidnight()); 
+7
source

You can get the current time stamp, set the clock to 24,

and subtract the old mark from the new.

 function beforeMidnight(){ var mid= new Date(), ts= mid.getTime(); mid.setHours(24, 0, 0, 0); return Math.floor((mid - ts)/60000); } 

alert (beforeMidnight () + 'minutes to midnight ")

+4
source

Source: https://habr.com/ru/post/1387292/


All Articles