0

I am trying to find time differences. Difference is NaN. What should I do?

currentTime.format() = 2016-12-07T11:43:19+03:00
pws.lastDataTime = 2016-12-07T08:35:14.4126931+00:00

var difference= currentTime.format() - pws.lastDataTime;
Vucko
  • 20,555
  • 10
  • 56
  • 107
diad
  • 41
  • 7
  • `currentTime.format()` - what type of object is this? - `pws.lastDataTime` and this ... do both return a `Date` object? or at least a `Number` that is at least somehow related to dates? – Jaromanda X Dec 07 '16 at 08:42
  • 3
    Possible duplicate of [Check time difference in Javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Vucko Dec 07 '16 at 08:42
  • @ Jaromanda X I editted my question – diad Dec 07 '16 at 08:44

2 Answers2

0
currentTime.format() = 2016-12-07T11:43:19+03:00

currentTime.format is a function. You can't assign it's return value to something.

currentTime.format() - pws.lastDataTime

I don't think the format function returns a number, but instead a string or an object. If you subtract anything from them, they return NaN (not a number). You need to either convert both to milliseconds and subtract one from the other, or calculate the year, month, day, hour, second and millisecond separately.

Bálint
  • 4,009
  • 2
  • 16
  • 27
0

I don't know what denomination you want, so I'll just show you how to find it in milliseconds.

If already you have a date or two, you can use date.getTime().

var stackOverflowLaunchDate = new Date(2008, 8, 15);
var today = new Date();
var diff = today.getTime() - stackOverflowLaunchDate.getTime(); // milliseconds since Stack Overflow was launched

If you don't have (and don't need) a date object, use Date.now() to get millisecondds since epoch

var start = Date.now();

// ... Some time later
var diff = Date.now() - start; // milliseconds since start
ArneHugo
  • 6,051
  • 1
  • 26
  • 47