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.