0

If I have two values, each representing a date such as YYYYMMDDHHMM (YearMonthDayHourMinute) like:

202012141800
202012141614

What I was trying to convey in the question is that this gives me 186 minutes, but this isn't accurate, however, since the last two digits will never be larger than 59 given 60 minutes in an hour. The 100 in 186 comes from hours 18 (6pm) and 16 (4pm).

How can I subtract these in Javascript to account for the extra 40 minutes tacked on if the two timestamps are more than an hour apart?

I have this, but it's not that efficient since I'd need to know the maximum number of hours two timestamps could be apart:

        var end_time = $('#the-agenda li.current time').data('end-time'),
            time_now = current_display_number,
            timer_duration = end_time - time_now;

        if (timer_duration > 200) {
            // if more than 2 hours, subtract 80 minutes
            timer_duration = timer_duration - 80;
        }
        else if (timer_duration > 100) {
            // if more than 1 hour, subtract 40 minutes
            timer_duration = timer_duration - 40;
        }

I feel like the answer may somehow be in this question's answer, but I am not sure how to apply that parseInt to this situation.

Nathan
  • 377
  • 1
  • 6
  • 16
  • 4
    Well `YYYYMMDDHHMM` is a *string* format. Never treat it as a number. For date arithmetic, store them as proper timestamps (e.g. minutes since epoch). If you get the string as input, [parse it](https://stackoverflow.com/q/5619202/1048572). – Bergi Dec 14 '20 at 21:35
  • Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – Heretic Monkey Dec 15 '20 at 12:41
  • @Bergi would you care to expand on "Never treat it as a number." Using the solution provided by pwilcox below, I was able to get this to work as expected. However, I had to first use .toString() to convert what I have to a string. – Nathan Dec 15 '20 at 14:28
  • 1
    @Nathan Is that jQuery you use to retrieve the `end_time`? [That's a problem](https://stackoverflow.com/q/10958047/1048572). (Converting it back to a string before doing anything with it should work, though). – Bergi Dec 15 '20 at 14:32
  • Yes @Bergi, that part is jQuery. The link you provided did provide the extra info there I was curious about. Thank you! – Nathan Dec 15 '20 at 18:29

2 Answers2

1
// Different in milliseconds
const difference = (new Date('2020-12-14T18:00:00')) - (new Date('2020-12-14T16:14:00'));

const inMinutes = Math.floor(difference / 60000);

You need to convert the string formats to a date object to get accurate date info.

James Hamann
  • 842
  • 6
  • 12
1

You wouldn't use parseInt. You would use Date.parse, except that the string has to be in a predefined format. Without using a specialized library, you'll have to parse the parts yourself and then create a new Date with the parts. Fortunately though the incoming strings seem straightforward to parse. Do something like this:

let startTimeStr = '202012141614';
let endTimeStr = '202012141800';

let asDateTime = (d) => new Date(
  d.substring(0,4), 
  d.substring(4,6) - 1, 
  d.substring(6,8),
  d.substring(8,10),
  d.substring(10,12)
)

let startTime = asDateTime(startTimeStr);
let endTime = asDateTime(endTimeStr);

let result = (endTime - startTime) / 60000;

console.log(result);
pwilcox
  • 5,542
  • 1
  • 19
  • 31
  • Perfect, thank you for that. For anyone else who may use this and run into any trouble, I was getting `d.substring error: “is not a function”` in console. This was because when I fed my variable into `startTimeStr`, it was a number, so using `startTimeStr = my_var.toString()` solved that. – Nathan Dec 15 '20 at 14:30