3

I write the code below:

var _MS_PER_Day=24*60*60*1000;
var utc1 = Date.UTC(1900, 1, 1);
var utc2 = Date.UTC(2014,11,16);
var x = Math.ceil((utc2 - utc1) / _MS_PER_Day);
alert(x);

I want to calculate the date difference between the two dates.The actual date difference is 41957 but after running my code i get 41956 ,one date less.What is wrong with my code ?

3
  • By my calculations the difference is 41958. Commented Nov 16, 2014 at 12:13
  • @nnnn convertunits.com/dates/from/Jan+1,+1900/to/Nov+16,+20 i check in more than 5 websites it is just 41957 day, i think urs also 1 day more. Commented Nov 16, 2014 at 12:16
  • Interesting: when I said "my calculations" I had just used Excel (2010), which seems to believe erroneously that 1900 was a leap year. So yes, 41957 is correct. Commented Nov 16, 2014 at 12:21

4 Answers 4

4

Your code is calculating the difference between Feb 1, 1900 and Dec 16, 2014 (41956 days). The value month should be between 0...11 (where 0 is January). Correct the month numbers to get the expected result:

var _MS_PER_Day = 1000 * 60 * 60 * 24;
var utc1 = Date.UTC(1900, 0, 1); // Jan 01, 1900
var utc2 = Date.UTC(2014, 10, 16); // Nov 16, 2014
var x = Math.ceil((utc2 - utc1) / _MS_PER_Day);
alert(x); // 41957
Sign up to request clarification or add additional context in comments.

3 Comments

1000 * 60 * 60 * 24 You used this twice. Why not just put in 86400000 or 864E5?
@BenjaBoy what result do you get then?
@BenjaBoy - The code in your question is not the same as this because you used different months.
2

This is an alternate code for the above which will give you 41957.

var date1 = new Date("1/1/1900");
var date2 = new Date("11/16/2014");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
alert(diffDays);

Reference : Get difference between 2 dates in javascript?

2 Comments

Your code works but i haven't exhaustively tested it.
Awesome. I've been doing it with math.round, but it's always giving me -0 and my logic was breaking. With this logic, I was able to fix my issues.
1

The months are from 0 to 11 considering 0 as January. You may also use getTime method to get the UTC timestamp in miliseconds.

var _MS_PER_Day = 1000 * 60 * 60 * 24;
var t1 = new Date(1900, 0, 1); // Jan 01, 1900
var t2 = new Date(2014, 10, 16); // Nov 16, 2014
var tdiff = Math.ceil((t2.getTime() - t1.getTime()) / _MS_PER_Day); // tdiff = 41957

http://www.w3schools.com/jsref/jsref_gettime.asp

Comments

0

I think you can use http://momentjs.com/ it give you the best result.

const a = new Date(YOUR_START_TIME),
      b = new Date()

let diff = moment([b.getFullYear(), b.getMonth(), b.getDate()])
  .diff(moment([a.getFullYear(), a.getMonth(), a.getDate()]), 'years', true)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.