0

I'm working on a front-end app, and was trying to calculate number of days between two dates, and the more I read on proper/accurate way of working out the days between two dates, the more I got confused.

So I thought, I shall post another question on here to finally shed some light on this confusion.

Also I was trying to workout when it would be safest to do the calculation; is it when I get the raw UTC date I get from the backend below, after they're converted to Locale date and time? What I mean by this, would it be better to get the diff between UTC dates or diff between Locale date and time?

Here are UTC dates:

UTC Date 1:  2014-11-30T15:41:51.012+0000
UTC Date 2:  2014-01-09T02:56:36.000+0000

Here are Locale dates (On Windows; this will be diff format on Linux):

Locale Date 1:  11/30/2014, 3:41:51 PM
Locale Date 2:  1/9/2014, 2:56:36 AM

So my questions are, which one of those would give accurate daysBetweenDates, UTC or Locale? Also how is this calculated, a JavaScript function snippet or demo would help me and others.

Thanks!

Simple-Solution
  • 4,209
  • 12
  • 47
  • 66
  • I don't really got the question, but UTC obviously has 3 digits after comma resolution, while second "locale" examples has only integer seconds resolution. Additionally, UTC has information about time zone. – Tommi Nov 30 '14 at 16:09
  • Have you tried to compare the results? What did you get? JavaScript stores dates in UTC, how a user will see a date depends on the toString method used. – Gosha_Fighten Nov 30 '14 at 16:10
  • possible duplicate of [How do I get the number of days between two dates in JavaScript?](http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript) –  Nov 30 '14 at 16:27

2 Answers2

1

Honestly, I'd use Moment.js

http://momentjs.com/docs/#/displaying/difference/

var a = moment('2014-11-30T15:41:51.012+0000');
var b = moment('2014-11-30T15:41:51.012+0000');
var dayDiff = a.diff(b, 'days');

You could do it natively, but this does it for you, it's lightweight, it'll handle all your locale woes and chances are you're going to want other date manipulation elsewhere that you can also use it for

JamesT
  • 2,988
  • 23
  • 29
  • @J Tolley - can you provide an example without using any library please. – Simple-Solution Nov 30 '14 at 16:12
  • Well no, sorry. The whole point of my answer is I'm suggesting you use a library and save yourself the headache. I'm sure there'll be another answer with sample code if you're desperate to do it natively. – JamesT Nov 30 '14 at 16:13
  • Thanks for your quick feedback. Also how accurate is your approach, not sure how this function does in the background, can you explain what it does in the background please. – Simple-Solution Nov 30 '14 at 16:25
1

This is how you would do it with pure JS:

var Date1 = new Date('2014-11-30T15:41:51.012+0000'.substr(0,10)).getTime();
var Date2 = new Date('2014-01-09T02:56:36.000+0000'.substr(0,10)).getTime();
var Diff = Math.floor(Math.max(Date1,Date2)-Math.min(Date1,Date2))/1000/60/60/24;
Jonathan Gray
  • 2,509
  • 15
  • 20
  • UTC or Locale version? and can you explain why would it give an accurate date and time diff in days? Also this return NaN – Simple-Solution Nov 30 '14 at 16:18
  • I don't think it matters but UTC would probably be a better preference. Basically you can think of Date1 and Date2 as being the amount of milliseconds since January 1 1970. By subtracting the initial date from the later date, you end up with the amount of milliseconds in time difference between the two dates. By converting this value to days and flooring the value, you end up with an accurate result. – Jonathan Gray Nov 30 '14 at 16:24
  • I just did this: `console.log('Diff UTC: ', Math.floor(('2014-11-30T16:27:16.059+0000'-'2014-01-09T02:56:36.000+0000')/1000/60/60/24));` and got `NaN` – Simple-Solution Nov 30 '14 at 16:29
  • Also, why are we not taking times into consideration, since that would make a difference over time when calculating diff in dates? – Simple-Solution Nov 30 '14 at 16:39
  • I removed them because I didn't think you needed that extra information. The entire format you've specified isn't directly compatible with JS so I did a quick implementation. – Jonathan Gray Nov 30 '14 at 16:43
  • @Simple-Solution I edited my answer again so you can use it without having to worry about the order of Date1 and Date2 but you may have to do some string manipulation to get it translated and working exactly how you want it. The first two lines simply create the date objects and hold the timestamp. The last line calculates the difference in days between them. – Jonathan Gray Nov 30 '14 at 16:47
  • What is the diff between `var Diff = Math.floor(Math.abs(Date1-Date2))/ONE_DAY;` and `var Diff = Math.floor(Math.max(Date1,Date2)-Math.min(Date1,Date2))/1000/60/60/24;`? – Simple-Solution Nov 30 '14 at 18:18
  • @Simple-Solution ONE_DAY isn't supported by native JavaScript. – Jonathan Gray Nov 30 '14 at 18:40
  • Sorry what I meant was diff between `Math.abs() and Math.max()-Math.min()`, but thanks for the ONE_DAY! – Simple-Solution Nov 30 '14 at 18:53
  • @Simple-Solution I completely forgot `Math.abs` existed. That should work fine! – Jonathan Gray Nov 30 '14 at 18:55