0

I'm calculating a difference between two times. This works fine in chrome and firefox, returning the milliseconds difference. It doesn't in Safari, returning difference as NaN.

    var currentdate = new Date();
    var myDate = "2018-07-15 21:06:00";
    var difference = (currentdate - new Date(myDate))/3600000;

Thanks

flowover9
  • 55
  • 1
  • 8
  • Technically that's not a valid ISO date format is why. Different browsers handle non standard formats differently – charlietfl Jul 16 '18 at 20:50
  • Possible duplicate of [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Bharata Jul 16 '18 at 21:55

1 Answers1

0

@charlietfl is correct. Try the ISO format

var currentdate = new Date();
var myDate = "2018-07-15T21:06:00.000Z";
var difference = (currentdate - new Date(myDate))/3600000;
Doug Coburn
  • 2,485
  • 27
  • 24
  • Hi @doug Coburn , that won't work. It messes up the difference across all browsers (reduces it). – flowover9 Jul 16 '18 at 21:22
  • @flowover9. Which browser is giving the incorrect result? I'm currently getting about 24.5 as a value for difference in chrome, safari, and firefox. – Doug Coburn Jul 16 '18 at 21:38