Comparing Two Dates

M

Mike N.

I am currently using a function to validate a form on the client side
(see code below). At the end of the function, I would like it to also
compare a startDate against an endDate to ensure that the endDate is
greater than (comes after) the startDate. The date format I'm using is
MM/DD/YYYY and it's writing to an MS SQL Server 2000 database table
via ASP. THANKS!!!

function ValidateForm()
{
//validate STATUS element
if (document.form.status[0].selected)
{
alert("Please select a Status");
document.form.status.focus();
return false;
}

//validate NAME element
if (document.form.name.value=="")
{
alert("Please enter a Name")
document.form.name.select();
return false;
}

//validate START DATE element
if ((document.form.startDate.value=="") ||
(document.form.startDate.value=="mm/dd/yyyy"))
{
alert("Please enter a valid Start Date")
document.form.startDate.focus();
return false;
}

//validate END DATE element
if ((document.form.endDate.value=="") ||
(document.form.endDate.value=="mm/dd/yyyy"))
{
alert("Please enter a valid End Date")
document.form.endDate.focus();
return false;
}

//validate DATES
//check to ensure that endDate is > startDate
}
 
T

Thomas 'PointedEars' Lahn

Mike said:
I am currently using a function to validate a form on the client side
(see code below). At the end of the function, I would like it to also
compare a startDate against an endDate to ensure that the endDate is
greater than (comes after) the startDate. The date format I'm using is
MM/DD/YYYY [...]

The Date() constructor takes the year, month and date as arguments (in this
order, among others) and creates Date objects which have a getTime() method
to reflect the number of milliseconds that have passed for this date since
January 1, 1970 00:00:00.

RegExp.exec() used on the input string will let you extract year, month and
date from it.

This has been explained in detail several times before. RTFM, RTFFAQ.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Wed, 28 Jul
2004 04:30:35, seen in Thomas 'PointedEars'
Lahn said:
Mike said:
I am currently using a function to validate a form on the client side
(see code below). At the end of the function, I would like it to also
compare a startDate against an endDate to ensure that the endDate is
greater than (comes after) the startDate. The date format I'm using is
MM/DD/YYYY [...]

The Date() constructor takes the year, month and date as arguments (in this
order, among others) and creates Date objects which have a getTime() method
to reflect the number of milliseconds that have passed for this date since
January 1, 1970 00:00:00.

Careless. You should be aware of the need to specify GMT or UTC here,
even though it will not affect the OP's use.
RegExp.exec() used on the input string will let you extract year, month and
date from it.

True, but not necessary.

Browsers in locations where it is appropriate to specify MM/DD/YYYY will
accept an MM/DD/YYYY string into new Date() .


Dates in the ISO 8601 date field order can be compared directly, if the
separators are constants, leading zeroes are used, the range 1000-9999
is not exceeded, and four-digit years are used. That AIUI is a FIPS and
ANSI standard, BTW.

Dates in the OP's form can easily be converted to ISO, by concatenating
substrings or using a RegExp; see below, and my datefmts.htm#RUSA, and
UStoISO8601(St) in my js-date9.htm.
 

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,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top