473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 15641
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 quick google gives <URL:http://www.merlyn.demon.co.uk/js-date1.htm#DYD>

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
JRS: In article <11**********************@g10g2000cwb.googlegroups .com>
, 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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
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).

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
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
xstatic
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 ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.