How to properly calculate remaining time in JS using getTime() from the two dates?
Asked Answered
V

3

8

I'm trying to calculate remaining time (ex: 10 years, 2 months and 10 days from today(2014/03/02) in JS using this function:

var d2 = new Date(2024, 3, 12);
var d1 = new Date();
var d0 = new Date(1970, 0, 1);

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) );
var years = diff.getFullYear();
var months = diff.getMonth();
var days = diff.getDay();

alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");

But instead of get the 10 years difference, I got 1980 years difference (though the days difference I understand that are produced buy the variation of days in months and years):

enter image description here

Is it possible to perform this "remaining time" operation using this strategy? If so, how to get the expected result?

Here the function in a JS shell: jsfiddle.net/3ra6c/

Vereen answered 2/3, 2014 at 17:34 Comment(6)
Once you have the difference of the 2 dates in milliseconds, you need to do some math to work out what that number of milliseconds means in term of years, days, minutes etc. You are likely to do approximatations unless you take leap years and seconds into account (which becomes complex) Calculating it in days would be diff / (24 * 60 * 60 *100) There are approx 86400000 milliseconds in a day. So on and so forth.Omnivorous
Lots of answers on SO, try searching something like stackoverflow.com/search?q=milliseconds+to+days Alternatively look at a library to do it for you, something like moments.js It can do such approximations.Omnivorous
Thanks, @Xotic750, I searched theses questions. I was just wondering way this operation through date manipulation wasn't working. Anyway, the only answer that I found similar to what I need was using the moment.js library in GAS. But it didn't work in my script.Vereen
Basically the Date object is not equipped with these type of functions.Omnivorous
Could you put it as an answer?Vereen
I'll try and give you an example with moments but there are already plenty of answers here on SO that describe the math.Omnivorous
O
5

As I explained, Date is not equipped with such functions and you have to do the math yourself. You can get the milliseconds difference between two dates, but then it's down to doing the math to represent that as you wish.

An example of using moment.js, loaded with require.js, with humanized approximations.

Javascript

require.config({
    paths: {
        moment: 'http://momentjs.com/downloads/moment.min'
    }
});

require(['moment'], function (moment) {
    var x = moment(new Date(2024, 3, 12)).from(new Date());

    console.log(x);
});

Output

in 10 years

On jsFiddle

Look at their docs to see how you can humanize the output, you may want a little more detail.

Omnivorous answered 2/3, 2014 at 18:4 Comment(0)
V
6

I find here the solution I was looking for:

var date1 = new Date();
var date2 = new Date(2015, 2, 2);
var diff = new Date(date2.getTime() - date1.getTime());

var years = diff.getUTCFullYear() - 1970; // Gives difference as year
var months = diff.getUTCMonth(); // Gives month count of difference
var days = diff.getUTCDate()-1; // Gives day count of difference

alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");

And it seems to work very well!

Vereen answered 2/3, 2014 at 21:32 Comment(1)
This will also give you some kind of approximation. Try the dates 2014-1-1 and 2014-1-31 and then try 2014-1-1 and 2014-2-1. Not quite what you'd expect? Just so long as you are aware. ;) Also be careful when like this, try 2014-1-1 and 2013-12-31Omnivorous
O
5

As I explained, Date is not equipped with such functions and you have to do the math yourself. You can get the milliseconds difference between two dates, but then it's down to doing the math to represent that as you wish.

An example of using moment.js, loaded with require.js, with humanized approximations.

Javascript

require.config({
    paths: {
        moment: 'http://momentjs.com/downloads/moment.min'
    }
});

require(['moment'], function (moment) {
    var x = moment(new Date(2024, 3, 12)).from(new Date());

    console.log(x);
});

Output

in 10 years

On jsFiddle

Look at their docs to see how you can humanize the output, you may want a little more detail.

Omnivorous answered 2/3, 2014 at 18:4 Comment(0)
C
1

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) )

Why do you add d0? Try to remove it.

Cathleencathlene answered 2/3, 2014 at 17:39 Comment(2)
Actually with or without d0 the result is the same (1980 years).Vereen
As stated above try to do some math / 1000 to get the seconds. Then divide by 3600 and floor the result. Then get your total seconds minus the floored hours in seconds. Now do the same for the minutes. Always start with the highest part. So if you want it in days hours seconds you need first to calculate the whole days. Then the whole hours and so on.Cathleencathlene

© 2022 - 2025 — McMap. All rights reserved.