1

Can someone explain how the milliseconds works and some documentation. I am trying to work out, given two dates, what is the years, days and months difference between the two dates. Ideally I would want to round off the number so no decimals. Is this possible in JavaScript without any libraries?

This is my code so far. I have tried doing 1000 * 60 / 365 and a few combinations but I do not know what those represent - I may try hours too.

        var date1 = new Date(a); // 01/03/2008
        var date2 = new Date(b); // 22/03/2016
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
        var diffMonths = Math.ceil(timeDiff / (12)); 
        var diffYears = Math.ceil(timeDiff / (365)); 
        console.log(diffDays);
        console.log(diffMonths);
        console.log(diffYears);

I managed to work out days difference but I still do not understand it fully.

TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

1 Answers1

2

This is a difficult think with plain JavaScript just because of the way that date-time is handled by the language. Your best solution would be to use the MomentJS library. It is really sleek, well documented and easy to use.

MomentJS

UPDATE

I am using a solution like this for people coming in the future from Moment.JS

var bornDate = moment([1990, 2, 12]); // get YYYY MM DD from datepicker etc
var userChoiceDate = moment([2016, 3, 22]);
var diffYears = bornDate.diff(userChoiceDate, 'years');
var diffDays = bornDate.diff(userChoiceDate, 'days');
console.log(diffYears);
console.log(diffDays);
Community
  • 1
  • 1
andre mcgruder
  • 1,120
  • 1
  • 9
  • 12
  • I understand wanting to do it in pure JS. However the things to think about are the difference in days in some months and leap years to say the lease. Dealing with dates is not a simple mathematical equation. The time, day, and year do not fall evenly. That is way we have leap year and Daylight Saving Time (some places). I tried to creating a solution that dealt with dateTime and it was a struggle. I was happy to find MomentJS. – andre mcgruder Mar 22 '16 at 21:07
  • Yes, it is a pretty wonderful library to be fair. I was being a bit paranoid and ignorant not wanting to use a library; I have used it in the past but it has become something of an amazing library + 1 – TheBlackBenzKid Mar 22 '16 at 21:14
  • This doesn't answer the question. Also, "*…because of the way that date-time is handled by the language*" is gratuitous criticism. There are issues with the Date object (parsing and formatting in the main), but it handles dates and times very logically and consistently. Issues with date arithmetic aren't the Date's fault, but with how humans measure days, months and years. – RobG Mar 22 '16 at 23:58
  • Yes. It's not that I was criticizing the language. It's not JS's fault. Plus most languages have have issues with date-time. Because of time itself. We cause get all Dr. Who here but that would probably involve Cyber Men. – andre mcgruder Mar 23 '16 at 02:14