-3

I want to calculate the difference between the current time and another date which I recuperate from another program. Unfortunalety, this second date is on ISO format ie. in something like that :

date2 = "2015-07-16T16:33:39.113Z"

I want to calculate the difference between this date2 and the current time and display the difference like that " 0h 53min 10s" for example. How can I do that in node.js please?

fujitsu4
  • 301
  • 1
  • 6
  • 15
  • 3
    Did you try anything at all? Did you even try to read any basic tutorial or documentation about JS dates? Or are you simply hoping us SO community will spoon-feed you? – Amit Jul 16 '15 at 21:10

1 Answers1

1

Try using moment. You get get the current time via the now() function call and manipulate dates with the framework (use diff()).

var moment = require('moment');

date2 = "2015-07-16T16:33:39.113Z"

var then = moment(date2, "YYYY-MM-DD'T'HH:mm:ss:SSSZ");
var now = moment();

var diff = moment.duration(then.diff(now));
if (diff < 0) {
    diff = Math.abs(diff);
}
var d = moment.utc(diff).format("HH:mm:ss:SSS");
console.log("Difference: " + d);

For reference, see Get the time difference between two datetimes

Community
  • 1
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – user2720864 Jul 17 '15 at 06:24
  • Understood, added a sample solution. Tested and working. – Ivan Smirnov Jul 18 '15 at 03:25