I've been searching for 4 hours now, and have not found a solution to get the difference between two dates in years, months, and days in JavaScript, like: 10th of April 2010 was 3 years, x month and y days ago.
There are lots of solutions, but they only offer the difference in the format of either days OR months OR years, or they are not correct (meaning not taking care of actual number of days in a month or leap years, etc). Is it really that difficult to do that?
I've had a look at:
- http://momentjs.com/ -> can only output the difference in either years, months, OR days
- http://www.javascriptkit.com/javatutors/datedifference.shtml
- http://www.javascriptkit.com/jsref/date.shtml
- http://timeago.yarp.com/
- www.stackoverflow.com -> Search function
In php it is easy, but unfortunately I can only use client-side script on that project. Any library or framework that can do it would be fine, too.
Here are a list of expected outputs for date differences:
//Expected output should be: "1 year, 5 months".
diffDate(new Date('2014-05-10'), new Date('2015-10-10'));
//Expected output should be: "1 year, 4 months, 29 days".
diffDate(new Date('2014-05-10'), new Date('2015-10-09'));
//Expected output should be: "1 year, 3 months, 30 days".
diffDate(new Date('2014-05-10'), new Date('2015-09-09'));
//Expected output should be: "9 months, 27 days".
diffDate(new Date('2014-05-10'), new Date('2015-03-09'));
//Expected output should be: "1 year, 9 months, 28 days".
diffDate(new Date('2014-05-10'), new Date('2016-03-09'));
//Expected output should be: "1 year, 10 months, 1 days".
diffDate(new Date('2014-05-10'), new Date('2016-03-11'));
I used this simple code to get difference in Years, Months, days with current date.
@RajeevPNadig's answer was what I was looking for, but the code returns incorrect values as written.
My edit to his answer got rejected and I was told to post it as my own answer. This can return some results that feel a little weird, but aren't technically incorrect.
Then you can use it like this:
Things can get a little weird if you call dateAgo with the current date and aren't using an YYYY-MM-DD string, i.e.:
If your use case is just date strings, then this should work pretty well for most people.
I have created, yet another one, function for this purpose:
Doesn't require any 3rd party libraries. Takes one argument -- date in YYYY-MM-DD format.
https://gist.github.com/lemmon/d27c2d4a783b1cf72d1d1cc243458d56
Yet another solution, based on some PHP code. The strtotime function, also based on PHP, can be found here: http://phpjs.org/functions/strtotime/.
Usage:
To calculate the difference between two dates in Years, Months, Days, Minutes, Seconds, Milliseconds using TypeScript/ JavaScript
However, you can update the above logic for the message to show seconds and milliseconds too or else use the object 'r' to format the message whatever way you want.
If you want to directly copy the code, you can view my gist with the above code here
Above snippet will print: Year: 1, Month: 4, Days: 2