convert multiple date formats and compare those dates

R

Richard Hollenbeck

I'm trying to write some code that will convert any of the most popular
standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan
1908) and compare the first with the second and calculate days, months, and
years. This is not for a college course. It's for my own personal
genealogy website. I'm stumped about the code. I'm working on it but not
making much progress. Is there any free code available anywhere? I know it
would take some serious coding to do all those conversions and validation
checking. Ideas? Suggestions? Many Thanks.

Rich
 
R

Richard Cornford

Richard Hollenbeck said:
I'm trying to write some code that will convert any of the most
popular standard date formats twice in to something like "dd Mmm
yyyy" (i.e. 08 Jan 1908) and compare the first with the second
and calculate days, months, and years. This is not for a college
course. It's for my own personal genealogy website. I'm stumped
about the code. I'm working on it but not making much progress.
Is there any free code available anywhere? I know it would take
some serious coding to do all those conversions and validation
checking. Ideas? Suggestions? Many Thanks.

The fatal stumbling block is "any of the most popular standard date
formats" because for the first 12 days in each month a DD/MM/YYYY date
format is indistinguishable from a MM/DD/YYYY format. Once you know the
format the rest is relatively easy, but if the first task is impossible
you will never find an implementation of it.

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Richard Cornford
The fatal stumbling block is "any of the most popular standard date
formats" because for the first 12 days in each month a DD/MM/YYYY date
format is indistinguishable from a MM/DD/YYYY format. Once you know the
format the rest is relatively easy, but if the first task is impossible
you will never find an implementation of it.

The OP ought to have read the newsgroup FAQ.

It is done in <URL:http://www.merlyn.demon.co.uk/js-date4.htm#VID>.

The OP did not specify full auto-recognition of date format !!

That page accepts four basic types of Gregorian date, choosing a method
in accordance with the button pressed.

D5+ expects Y+MMDD e.g. 20030215 030215
ISO expects Y+_M+_D+ e.g. 2003-2-15 2003 Feb 15
EU expects D+_M+_Y+ e.g. 15 Okt, 2003 15.02.03
NA expects M+_D+_Y+ e.g. Feb 15th, 2003 2/15/03


It does not, or does not yet, allow auto-recognition of [YYYY, month-in-
letters, day cardinal or ordinal] in any order, but that should not be
too hard.
 
R

Richard Hollenbeck

Dr. Stockton,
Thank you very much!

I was unaware of http://www.merlyn.demon.co.uk/ but I went there and am
studying it now. I downloaded a snippet of existing code from "The
JavaScript Source" and attempted to modify it. I'm learning! Thank you for
your help. The program works pretty well but seems to make minor
calculation errors. For example, my grandmother was born on January 8, 1908
and died on January 27, 2004. The program calculated her age as 96 years, 4
months, 3 weeks, 6 days. See? January to January shouldn't have an extra 4
months in it--should it?

Here is the code I came up with after my modifications:


<HTML>
<HEAD>

<TITLE>Age Calculator</TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!--
// Originally a date comparison program written by Ronnie T. Moore
// Web Site: The JavaScript Source
// Modified into age calculator by Rich Hollenbeck on 2/10/2004

// Functions include:
// isValidDate(dateStr),
// dateDiff(dateform)


function isValidDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit
year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}

month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day ==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
}
}
return true;
}


function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff = new Date();

if (isValidDate(dateform.firstdate.value)) { // Validates first date
date1temp = new Date(dateform.firstdate.value);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits


if (isValidDate(dateform.seconddate.value)) { // Validates second date
date2temp = new Date(dateform.seconddate.value);
date2.setTime(date2temp.getTime());
}
else return false; // otherwise exits


// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);


years = 0; // declare a "years" variable
months = 0; // declare a "months" variable

if (weeks >= 52){
while (weeks >= 52){
years ++;
weeks -= 52;
}
}

if (weeks >= 4){
while (weeks >= 4){
months ++;
weeks -= 4;
}
}

if (months >=12) {
while (months >= 12){
years += 1;
months -= 12;
}
}


if (years == 1){yearName = "year";}
else yearName = "years";

if (months == 1){monthName = "month";}
else monthName = "months";

if (weeks == 1) {weekName = "week";}
else weekName = "weeks";

days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);

if (days == 1) {dayName = "day";}
else dayName = "days";

dateform.difference.value = years + " " + yearName + ", " + months + " " +
monthName + ", " + weeks + " " + weekName + ", " + days + " " + dayName +
".";


return false; // form should never submit, returns false
}

// End -->
</script>
</HEAD>




<BODY BGCOLOR="#BBBBBB">

<TABLE WIDTH=600 ALIGN="CENTER" CELLPADDING=30 BORDER=0>

<tr><td BGCOLOR="#DDDDDD">

<BASEFONT FACE="Arial" SIZE=3><STRONG>

<P ALIGN="CENTER">Age Calculator<br>under construction (makes calculation
errors)</P>

<FORM ID="inputDates" name="inputDates" onSubmit="return dateDiff(this);">

<P>BIRTH DATE:<BR>
<INPUT TYPE=text ID="firstdate" name="firstdate" value="" size=10
maxlength=10> (mm / dd / yyyy)</P>


<P>DEATH DATE:<BR>
<INPUT TYPE=text name=seconddate value="" size=10 maxlength=10> (mm / dd /
yyyy)</P>


<P><INPUT TYPE=submit value="Calculate"></P>

<P>LIFESPAN:<br>
<INPUT TYPE=label name=difference value="" size=35></P>
</form>

<script type="text/javascript"><!--
document.inputDates.firstdate.focus();
//--></script>

<P><form>
<input type=button value="Close This Window"
onClick="javascript:window.close();">
</form>
</P>

</STRONG>
</BASEFONT>
</TD></TR>
</FORM>
</TABLE>
</BODY>
</HTML>









----- Original Message -----
From: "Dr John Stockton" <[email protected]>
Newsgroups: comp.lang.javascript
Sent: Wednesday, February 11, 2004 3:19 PM
Subject: Re: convert multiple date formats and compare those dates

JRS: In article <[email protected]>, seen in
Richard Cornford
The fatal stumbling block is "any of the most popular standard date
formats" because for the first 12 days in each month a DD/MM/YYYY date
format is indistinguishable from a MM/DD/YYYY format. Once you know the
format the rest is relatively easy, but if the first task is impossible
you will never find an implementation of it.

The OP ought to have read the newsgroup FAQ.

It is done in <URL:http://www.merlyn.demon.co.uk/js-date4.htm#VID>.

The OP did not specify full auto-recognition of date format !!

That page accepts four basic types of Gregorian date, choosing a method
in accordance with the button pressed.

D5+ expects Y+MMDD e.g. 20030215 030215
ISO expects Y+_M+_D+ e.g. 2003-2-15 2003 Feb 15
EU expects D+_M+_Y+ e.g. 15 Okt, 2003 15.02.03
NA expects M+_D+_Y+ e.g. Feb 15th, 2003 2/15/03


It does not, or does not yet, allow auto-recognition of [YYYY, month-in-
letters, day cardinal or ordinal] in any order, but that should not be
too hard.
links.

Dr John Stockton said:
JRS: In article <[email protected]>, seen in
Richard Cornford
The fatal stumbling block is "any of the most popular standard date
formats" because for the first 12 days in each month a DD/MM/YYYY date
format is indistinguishable from a MM/DD/YYYY format. Once you know the
format the rest is relatively easy, but if the first task is impossible
you will never find an implementation of it.

The OP ought to have read the newsgroup FAQ.

It is done in <URL:http://www.merlyn.demon.co.uk/js-date4.htm#VID>.

The OP did not specify full auto-recognition of date format !!

That page accepts four basic types of Gregorian date, choosing a method
in accordance with the button pressed.

D5+ expects Y+MMDD e.g. 20030215 030215
ISO expects Y+_M+_D+ e.g. 2003-2-15 2003 Feb 15
EU expects D+_M+_Y+ e.g. 15 Okt, 2003 15.02.03
NA expects M+_D+_Y+ e.g. Feb 15th, 2003 2/15/03


It does not, or does not yet, allow auto-recognition of [YYYY, month-in-
letters, day cardinal or ordinal] in any order, but that should not be
too hard.
links.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Richard Hollenbeck <richard.hollenbeck@verizo
n.net> posted at Thu, 12 Feb 2004 02:20:56 :-
I was unaware of http://www.merlyn.demon.co.uk/ but I went there and am
studying it now. I downloaded a snippet of existing code from "The
JavaScript Source" and attempted to modify it. I'm learning! Thank you for
your help. The program works pretty well but seems to make minor
calculation errors. For example, my grandmother was born on January 8, 1908
and died on January 27, 2004. The program calculated her age as 96 years, 4
months, 3 weeks, 6 days. See? January to January shouldn't have an extra 4
months in it--should it?

Here is the code I came up with after my modifications:

None of that is worth saving. The algorithm is ill-conceived; it seems
to be based on 52 weeks to a year, 12 months to a year, and four weeks
to a month. It ignores the variation in the length of the civil day.

Since the number of days in a month and of days in a year are variable,
and of weeks in a year is variable and perhaps non-integer, the result
must to some extent be arbitrary.

The calculation needs to be done by the methods that we were taught in
school for the subtraction of money or weight amounts, in which each
field is to a different base; but in this case the base of the days
field is variable.

Subtraction of date objects must necessarily fail to give "obviously-
expected" results - out of every 1461 people in an infinite randomly-
born population, on the second recurrence of their birth date 730 will
be 730 days old, 730 will be 731 days old, and one will be 2922 days
old. Moreover, the age in days at the halfth birthday varies by at
least three days, and the age in hours has an additional variation of
+-1.


Read about dates themselves; read about javascript dates, and read the
newsgroup FAQ taking particular note of news article formatting, in 2.3.

The difference of those dates is 96 years, 0 months and 19 days (2 weeks
// Modified into age calculator by Rich Hollenbeck on 2/10/2004

In an international medium, you need to write dates so that they are
correctly understood in countries other than your own. ISO-8601 calls
for that to be written as 2004-02-10; but, if you cannot bring yourself
to use the rational order, Feb 10 2004 would be acceptable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top