0

What's the most concise, performant way to get in Javascript the minutes remaining between now, and the upcoming day at 01:00 (am)?

Then, once the current time is after 01:00, I start calculating the difference to the next.

Luke
  • 2,976
  • 6
  • 34
  • 44
  • You have to be precise in your question. What do you mean 'or perhaps already current'? Also, have you tried anything yet? You can get the current time with `var now = new Date()` – TKoL Jun 03 '19 at 08:55
  • 1
    Possible duplicate of [Check time difference in Javascript](https://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Mark Baijens Jun 03 '19 at 08:56
  • @TKoL if current time is `00:30`, I would need to calculate the difference on the same day. If current time is after `1:00`, start calculating the difference to the next. – Luke Jun 03 '19 at 09:00
  • I'll describe to you the logic, and you can figure out how to implement it then. What i would do is make two Date variables, one for now and one for 'one o'clock'. On the one for one o'clock, use 'setHours' `oneOclock.setHours(1,0,0,0)` to set it to 1 AM. Then, check if that date is in the past relative to now, if it is in the past add one day to it. Then you can just subtract the dates from each other to get the difference. – TKoL Jun 03 '19 at 09:04

4 Answers4

3

in javascript, a specified date can be provided like this

var date1 = new Date('June 6, 2019 03:24:00');

or it can be specified like this

var date2 = new Date('2019-6-6T03:24:00');

javascript can natively subtract 2 dates

console.log(date1 - date2);
//expected 0;

using this method will output the difference in the dates in milliseconds, to get minutes you'll want to divide the value by 60000;

so

var futureTime = new Date('2019-06-06T07:24:00');
//there must be a 0 infront of 1 digit numbers or it is an invalid date
var now = new Date();
var difference = (futureTime - now) / 60000;
//get minutes by dividing by 60000
//doing Date() with no arguments returns the current date

read about the javascript Date object here for more information https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Joshua Usi
  • 104
  • 1
  • 9
  • Parsing of `new Date('June 6, 2019 03:24:00')` is implementation dependent, so while you *can* do that, it's not a good idea. ;-) – RobG Jun 04 '19 at 06:04
2
let now = new Date();
let next1am = new Date();
next1am.setHours(1, 0, 0, 0); // same as now, but at 01:00:00.000
if (next1am < now) next1am.setDate(next1am.getDate() + 1); // bump date if past
let millisecondDiff = next1am - now;
let minuteDiff = Math.floor(millisecondDiff / 1000 / 60);
Amadan
  • 191,408
  • 23
  • 240
  • 301
0

you can you moment.js here

var current = new Date()
var end = new Date(start.getTime() + 3600*60)// end time to calculate diff

var minDiff = end - start; // in millisec
Harish
  • 1,841
  • 1
  • 13
  • 26
0

You can calculate by pure JavaScript:

let today = new Date();
let [y,M,d,h,m,s] = '2019-06-04 05:00:11'.split(/[- :]/);
let yourDate = new Date(y,parseInt(M)-1,d,h,parseInt(m)+30,s);
let diffMs = (yourDate - today);
let diffDays = Math.floor(diffMs / 86400000); // days
let diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
let diffMins = (diffDays * 24 * 60) 
               +  (diffHrs *60) 
               +  Math.round(((diffMs % 86400000) % 3600000) / 60000); // The overall result
                                                                       // in minutes

In, addition avoid using the built–in parser for any non–standard format, e.g. in Safari new Date("2019-04-22 05:00:11") returns an invalid date. You really shouldn't even use if for standardized formats as you will still get unexpected results for some formats. Why does Date.parse give incorrect results?

StepUp
  • 36,391
  • 15
  • 88
  • 148