0

I converted my start and end date to .ToLocalString and now I am trying to use math.abs to calculate difference between start and end date in numbers but its Value is NaN. Any suggestions on how to apply Math.abs in this situation are appreciated.

Note: Start date will be in EDT and EndDate will be in EST. But they might or might not be in same timezone.

var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.toLocaleString();

var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.toLocaleString();



var timeDiff = Math.abs(endDate - startDate);  //This is NaN
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));  // Here it will add 1 extra day. Example: 11/06/2018 - 11/04/2018 = 2 days but this gives 3days are timezone change on 11/04/2018 and thats the issue.
Programmermid
  • 588
  • 3
  • 9
  • 30
  • You're trying to subtract strings. Strings do not support arithmetic. –  Aug 28 '18 at 17:46
  • 1
    @Amy I am trying to find a way where I can get difference in days with ignoring timezones. If I change it to GetTime or UTCString etc. it adds up extra hour on the last & first day of light saving. – Programmermid Aug 28 '18 at 17:58
  • How are you determining that it's adding an extra hour? Please show that code. –  Aug 28 '18 at 17:59
  • 1
    @Amy I added code to my question. It adds up one entire day when startdate is in EDT and EndDate is in EST. – Programmermid Aug 28 '18 at 18:04
  • Take a look at this answer to [How do I get the number of days between two dates in JavaScript?](https://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript#11252167) - addresses DST and timezones in detail (check out the other answers for simpler approaches as well). – benvc Aug 29 '18 at 19:13

1 Answers1

0

I would use the unix timestamp.

var startDate1 = new Date(homeCtrl.createStartDate);
var startDate = startDate1.getTime();

var endDate1 = new Date(homeCtrl.createEndDate);
var endDate = endDate1.getTime();


var timeDiff = Math.abs(endDate - startDate);
nbwoodward
  • 2,816
  • 1
  • 16
  • 24
  • 1
    This adds an additional day on the last day of daylight saving and first day of day light saving. I want to ignore timezone factor. – Programmermid Aug 28 '18 at 17:55
  • Aren't the original dates in the same timezone? –  Aug 28 '18 at 17:58
  • 1
    @Amy In one scenario Start date will be in EDT and EndDate will be in EST. But they might or might not be in same timezone. – Programmermid Aug 28 '18 at 17:59
  • @Programmermid I think that's an important detail that should be included in the question... –  Aug 28 '18 at 18:00
  • What do `homeCtrl.createStartDate` and `homeCtrl.createEndDate` look like? Are you sure it has a problem with daylight savings time? The definition of .getTime() is "A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date." Which should be immune to timezones. – nbwoodward Aug 28 '18 at 18:04
  • @nbwoodward Try in this scenario it seems its not immune. – Programmermid Aug 28 '18 at 18:06
  • 1
    I think you should ask your question again with more information and a demonstration of the issue with daylight savings time. – nbwoodward Aug 28 '18 at 18:09