472,799 Members | 1,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

convert multiple date formats and compare those dates

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
Jul 20 '05 #1
4 5264
"Richard Hollenbeck" <ri****************@verizon.net> wrote in message
news:ce******************@nwrddc02.gnilink.net...
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.
Jul 20 '05 #2
JRS: In article <c0*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Wed, 11 Feb 2004 16:41:33 :-
"Richard Hollenbeck" <ri****************@verizon.net> wrote in message
news:ce******************@nwrddc02.gnilink.net. ..
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.


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #3
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" <sp**@merlyn.demon.co.uk>
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 <c0*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Wed, 11 Feb 2004 16:41:33 :-
"Richard Hollenbeck" <ri****************@verizon.net> wrote in message
news:ce******************@nwrddc02.gnilink.net. ..
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.


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 © <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:lS**************@merlyn.demon.co.uk... JRS: In article <c0*******************@news.demon.co.uk>, seen in
news:comp.lang.javascript, Richard Cornford
<Ri*****@litotes.demon.co.uk> posted at Wed, 11 Feb 2004 16:41:33 :-
"Richard Hollenbeck" <ri****************@verizon.net> wrote in message
news:ce******************@nwrddc02.gnilink.net. ..
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.


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 © <URL:http://jibbering.com/faq/> Jim Ley's FAQ for 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.
Jul 20 '05 #4
JRS: In article <cq****************@nwrddc02.gnilink.net>, seen in
news:comp.lang.javascript, 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
5 days) <URL:http://www.merlyn.demon.co.uk/js-date2.htm#DYMD>.

// 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.
--
© 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.
Jul 20 '05 #5

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

Similar topics

5
by: Dominique Javet | last post by:
Hello, I'm new to php and mysql and I use Dreamweaver MX 2004, so sorry for this "newbie" question... I've found no answer in the forum ... I've a date problem with my formular. In my mysql DB...
4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
2
by: Scott Knapp | last post by:
Good Day - I have a form which sets the current date, as follows: <script type="text/javascript"> xx=new Date() dd=xx.getDate() mm=xx.getMonth()+1 yy=xx.getYear() mmddyy=mm+"/"+dd+"/"+yy...
2
by: Julie Wardlow | last post by:
Help! I am calculating a future date using the DateAdd function in a query (the calculation also involves an IIf statement), and have managed to get this formula to produce the required result....
7
by: Nimmy | last post by:
Hi, I have a file which has different dates, I want to scanf them as CHAR and convert them to DATE format, how can I do this? Thanks
3
by: Tiya | last post by:
Hi there !!! I would like to know how to compare dates in javascript. var sdate = new Date(theform.SubmissionDate.value); var odate = new Date(theform.StartDate.value); var todaysdate = new...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.