Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Comparison Problem 2

Status
Not open for further replies.

Mighty

Programmer
Feb 22, 2001
1,682
US
Guys,

This forum seems to be the saviour for all my problems. I have a validation function which needs to compare the date entered in a form field with the current date. The format of the date entered is "dd-MON-yyyy" e.g. 14-MAR-2001

Can someone please tell me how I can use JS to determine whether or not a date entered in such a format is greater than the current date. Mise Le Meas,

Mighty :)
 
this should work:

function checkDate(date)
{
var x = date.split("-");
switch(x[1])
{
case "JAN":
x[1] = 1;
break;
case "FEB":
x[1] = 2;
break;
case "MAR":
x[1] = 3;
break;
case "APR":
x[1] = 4;
break;
case "MAY":
x[1] = 5;
break;
case "JUN":
x[1] = 6;
break;
case "JUL":
x[1] = 7;
break;
case "AUG":
x[1] = 8;
break;
case "SEP":
x[1] = 9;
break;
case "OCT":
x[1] = 10;
break;
case "NOV":
x[1] = 11;
break;
case "DEC":
x[1] = 12;
break;
}
var y = new Date();
var z = new Date(x[1]+"/"+x[0]+"/"+x[2])
if(z > y){return true;}
return false;
} luciddream@subdimension.com
 
this would be more elegant :
---
var month_table = new Array ["JAN", "FEB", ....]
var x = date.split("-")
for (i=0; i < month_table.length; i++)
if (month_table==x[1])
x[1]=i+1

if (!isNan(x[1])
{
var y = new Date();
...
}
----
(luciddream, i'm not saying your code is inelegant or it's not good ... i'm just feeling like adding elegance in code today :) - well the summer collections defiles have started !)




}
 
!! that was : if (month_table==x[1])
not if (month_table==x[1])

 
s'ok, i was in a bulky and inefficient mood when i wrote it. luciddream@subdimension.com
 
Thanks Guys,

A combination of both suggestions worked a treat.

:) Mise Le Meas,

Mighty :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top