473,233 Members | 2,315 Online

# JavaScript Date Difference, accounting for Leap Years

Hi,

I'm really sorry to post this as I know it must have been asked
countless times before, but I can't find an answer anywhere.

Does anyone have a snippet of JavaScript code I could borrow which
calculated the difference in years and days between two dates, and
takes leap years into account?

I'm calculating the difference in the usual way, i.e....

var difference = dateTo.getTime() - dateFrom.getTime();

....and converting this millisecond value into days by using...

var daysDifference = (difference/1000/60/60/24);

But how do I then display the difference in days AND years? I've tried
the following:

var yearsDifference = Math.floor(daysDifference/365.25);
var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25));

....but it gives me inaccuracies. For example, if I use my code to
calculate the difference between 05/01/1998 and 05/01/2000 it returns 1
year and 364 days!

Any assistance would be gratefully received!

Jun 19 '06 #1
4 15577
ja********@hotmail.com writes:
Does anyone have a snippet of JavaScript code I could borrow which
calculated the difference in years and days between two dates, and
takes leap years into account?
That requires a more precise description of what you want.
I'm calculating the difference in the usual way, i.e....

var difference = dateTo.getTime() - dateFrom.getTime();

...and converting this millisecond value into days by using...

var daysDifference = (difference/1000/60/60/24);
This fails if the dates are using local time and the difference
crosses a daylight saving time boundary (i.e., a day that is not
24 hours long). At least round it to the nearest integer.
But how do I then display the difference in days AND years?
How many days and years are there between
2004-02-28 and 2005-02-28 ?
How many days and years between
2004-02-29 and 2005-02-28 ?
How many days and years between
2004-02-29 and 2005-03-01 ?

My immediate guesses would be, respectively:
one year, zero days
zero years, 365 days
one year, one day

How come moving both the start date and the end date one day forwards
changes the distance between them?

And what is the day that is one year and zero days after 2004-02-29?
If there is no day that is one year and zero days after 2004-02-29,
how long is a year?
The problem I'm trying to illustrate is that you are talking about
periods of some years and some days, when years are not a fixed number
of days.

I've tried the following:

var yearsDifference = Math.floor(daysDifference/365.25);
var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25));

...but it gives me inaccuracies.
That's a matter of definition :)
For example, if I use my code to calculate the difference between
05/01/1998 and 05/01/2000 it returns 1 year and 364 days!
Unsurpricingly. You are defining a year to be 365.25 days. The
exact result would be a difference of one year and 364.75 days.
Then you decide to round the days, but "one year" is still not
a whole number of days.
Any assistance would be gratefully received!

What do you need it for?

A specification of an algorithm could be:

One whole number of years after a specific date ends on the same date
on a later year, or, if that date does not exist (i.e., it's Feb
29th), the first following date that does (Mar 1st).

The distance in years and days between two dates is the largest
number of whole years after the earlier date that is still less than
the larger date, plus the number of days from that date to the end
date.

So, algorithm:
----
function YDDiff(years,days) {
this.years = years;
this.days = days;
}
YDDiff.prototype.toString = function() {
return this.years + "y" + this.days +"d";
};

function diffYearAndDate(d1,d2) {
// ensure d1 <= d2
if (d1 > d2) { return diffYearAndDate(d2,d1); }

// find n whole years later than d1 in d2's year.
var dt = new Date(d1);
dt.setFullYear(d2.getFullYear());
// n whole years later > d2
var overflow = (dt > d2);
// max whole years later than d1 less than d2
dt = new Date(d1);
dt.setFullYear(d2.getFullYear() - overflow);
// whole years from d1 to dt
var years = dt.getFullYear() - d1.getFullYear();
// days from dt to d2, less than whole year from dt
var days = Math.round((d2 - dt)/864e5);
return new YDDiff(years,days);
}
----

Now you'll just have to see if that is what you really need :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 19 '06 #2
, dated Mon, 19 Jun 2006 08:06:17 remote, seen in
news:comp.lang.javascript, ja********@hotmail.com posted :
I'm really sorry to post this as I know it must have been asked
countless times before, but I can't find an answer anywhere.
Then you are an unskilful looker.
Does anyone have a snippet of JavaScript code I could borrow which
calculated the difference in years and days between two dates, and
takes leap years into account?

I'm calculating the difference in the usual way, i.e....

var difference = dateTo.getTime() - dateFrom.getTime();
Usual? but nowadays many people know better. That gives the difference
in absolute time (ignoring Leap Seconds).
...and converting this millisecond value into days by using...

var daysDifference = (difference/1000/60/60/24);
864e5 is easier to write than 1000/60/60/24. However, one cannot rely
on civil days all being of that length.
But how do I then display the difference in days AND years? I've tried
the following:

var yearsDifference = Math.floor(daysDifference/365.25);
var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25));

...but it gives me inaccuracies. For example, if I use my code to
calculate the difference between 05/01/1998 and 05/01/2000 it returns 1
year and 364 days!
But what are those dates? This is an international newsgroup, and dates
need to be presented unambiguously. Generally, the daycount difference
for those dates is 730 days; but where FFF is used it is 731 days.
Any assistance would be gratefully received!

You should have read the newsgroup FAQ before asking. Be aware that
Google did not invent newsgroups, which was done long before the Web
appeared; they merely provide an inferior interface, and do not give
adequate guidance in the established use of News.
Since the number of days in a year is not constant, there can be no one
correct answer. One can count the full years in the interval from the
beginning, and see how many days are left. Or one can do it in reverse.
Or one can determine the daycount difference, and do some approximation
to a rounded div/mod 365.25 or 365.2425. Or ...? The different methods
will, for at least some date combinations, give different answers. One
method is in js-date1.htm on my site.

If this is coursework, then, if the instructor is intelligent (one
cannot rely on that), the point of interest should be how thoughtfully
you treat the difficulties.

But if it is a real-world application, the originators of the situation
should have given an unambiguous indication of how the results should be
obtained.

--
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
Jun 19 '06 #3
JRS: In article <1w**********@hotpop.com>, dated Mon, 19 Jun 2006
20:46:04 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com> posted :
function diffYearAndDate(d1,d2) {
// ensure d1 <= d2
if (d1 > d2) { return diffYearAndDate(d2,d1); }

// find n whole years later than d1 in d2's year.
var dt = new Date(d1);
dt.setFullYear(d2.getFullYear());
// n whole years later > d2
var overflow = (dt > d2);
// max whole years later than d1 less than d2
dt = new Date(d1);
dt.setFullYear(d2.getFullYear() - overflow);
// whole years from d1 to dt
var years = dt.getFullYear() - d1.getFullYear();
// days from dt to d2, less than whole year from dt
var days = Math.round((d2 - dt)/864e5);
return new YDDiff(years,days);
}

Using dt = new Date(d1) does not always give an Object dt
representing the same date as d1, since 1900 years may be added.

I think it noticeably slower than dt = new Date(+d1)
but dt = new Date(d1.valueOf()) has seemed slightly faster still.
When using Date Objects, one must be careful of Time :

diffYearAndDate(new Date(2000, 1, 1, 07), new Date(2001, 1, 1, 12))
-> 1y0d
diffYearAndDate(new Date(2000, 1, 1, 17), new Date(2001, 1, 1, 12))
-> 0y366d

though it is at least arguable that the calculation *should* do that,
the difference might be unexpected.

I've put a version of that in
<URL:http://www.merlyn.demon.co.uk/js-date1.htm#DYD> and have as yet
found no disagreement with the existing code which starts with date
strings and makes less use of Date Objects (the test is flawed (at least
in IE4) for years 0..69 AD).

--
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jun 20 '06 #4
Dr John Stockton <jr*@merlyn.demon.co.uk> writes:
Using dt = new Date(d1) does not always give an Object dt
representing the same date as d1, since 1900 years may be added.
Indeed. My mistake was assuming the conversion to a primitive
value would become a number, not a string. After checking, I
can see that is not the case.
I think it noticeably slower than dt = new Date(+d1)
but dt = new Date(d1.valueOf()) has seemed slightly faster still.
The latter is also a little more readable.
When using Date Objects, one must be careful of Time :

diffYearAndDate(new Date(2000, 1, 1, 07), new Date(2001, 1, 1, 12))
-> 1y0d
diffYearAndDate(new Date(2000, 1, 1, 17), new Date(2001, 1, 1, 12))
-> 0y366d

though it is at least arguable that the calculation *should* do that,
the difference might be unexpected.

True. A more reasonable behavior would be to ignore time by doing
setHours(0,0,0,0) on the dates before starting the calculation.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 13 by: Hussein Patwa | last post by: Hi there. I'm new to this group. I'm also partially sighted so navigating the web is sometimes quite difficult. I'm looking for a javascript date picker, you know the ones that travel sites... 7 by: Ben | last post by: Hi, What is the synatx in C# for calculating date difference between 2 dates in terms of no. of days? Thanks, Ben 5 by: Simon Dean | last post by: Probably being a little thick here, but when you subtract one date away from another, how do you convert the resultant value into a number of days... I guess I could easily / 60 / 60 / 24... but... 3 by: pmarisole | last post by: The following javascript code gives me the date validation that I need except after the correct date is entered into the field, it puts the date in the wrong format EXAMPLE: User enters... 1 by: rockysg | last post by: I need to create a page which takes input from a field from MS Access Database which has one date and there is another field in the MS Access Database which has another date. I want to find the... 4 by: xstatic | last post by: After searching through the hundreds of links about "Javascript Date Formatting", all I am finding is how to format dates that gives a "current date" result. Here is what I need... I have a... 7 by: rdawadiuk | last post by: hi, I would like to calculate the date difference between two dates using field names which I have in my table. The field name is " Account opened on" for eg If the "Account open on" is Jan 5,... 2 by: DAHMB | last post by: I am trying to write a code that will: check an employees date of hire field compare it to todays date to get a years of service number then check the emoployees rank and if the rank equal 1... 10 by: sarah2855 | last post by: I have a table called Customers, This table has CustomerName, Order, and Order Date: Customer1 Order A 1/1/2006 Order B 1/6/2006 Order C ... 3 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In... 0 by: jianzs | last post by: Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from... 0 by: abbasky | last post by: ### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE... 0 by: egorbl4 | last post by: Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ... 1 by: davi5007 | last post by: Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the... 0 by: MeoLessi9 | last post by: I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines".... 0 by: Aftab Ahmad | last post by: Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on... 0 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...