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):
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/3ra6c/
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):
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/
Share Improve this question edited Mar 2, 2014 at 18:19 Xotic750 23.5k8 gold badges59 silver badges81 bronze badges asked Mar 2, 2014 at 17:34 craftApprenticecraftApprentice 2,77720 gold badges61 silver badges87 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 6I 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!
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.
var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) )
Why do you add d0? Try to remove it.
moments.js
It can do such approximations. – Xotic750 Commented Mar 2, 2014 at 17:47moment.js
library in GAS. But it didn't work in my script. – craftApprentice Commented Mar 2, 2014 at 17:52Date
object is not equipped with these type of functions. – Xotic750 Commented Mar 2, 2014 at 17:55