-1

I have two different date format I need to get time difference of. I tried;

clDateTimestamp: Date; //has timestamp in such format; 2020-20-22T16:15:43.110+000
clDate = new Date(clDateTimestamp); // returns Invalid Date;
currentDate = new Date(Date.now()); // returns date in such format; Sat Apr 25 2020 17:32:53 GMT-500
dateDiference = currentDate.getTime() - clDate.getTime(); //returns NaN

Any ideas on how return the time difference even in milliseconds?

ahmeto
  • 33
  • 4
  • If *clDateTimestamp* is a Date object, then the difference in milliseconds is simply `Date.now() - clDateTimestamp`. If it's a string like "2020-20-22T16:15:43.110+000" then you must parse it manually as that format is not supported by ECMA-262 and will return an invalid date in some implementations at least (where "manually" includes using a library if you want). See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Apr 26 '20 at 02:41

1 Answers1

0

Use

Date.now() - Date.parse(clDateTimestamp)

That should provide the difference in milliseconds. Make sure clDateTimestamp is in a correct date format.

Josh
  • 827
  • 5
  • 7