Re: dd/mm/yyyy Date Compare Problem
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: dd/mm/yyyy Date Compare Problem

From: Dr John Stockton <jrs@merlyn.demon.co.uk>
Date: Fri Apr 28 2006 - 22:20:24 CEST

JRS: In article <1146231504.278277.155500@y43g2000cwc.googlegroups.com>
, dated Fri, 28 Apr 2006 06:38:24 remote, seen in
news:comp.lang.javascript, Assimalyst <c_oxtoby@hotmail.com> posted :

>I have a working script that converts a dd/mm/yyyy text box date entry
>to yyyy/mm/dd

AFAICS, you do not. It converts "dd/mm/yyyy" to a Date Object, but
yyyy/mm/dd is not involved.

> and compares it to the current date, giving an error
>through an asp.net custom validator, it is as follows:
>
>function doDateCheckNow(source, args)
>{
> var oDate = document.getElementById(source.controltovalidate); //
>dd/mm/yyyy

Don't let your posting agent line-wrap; posted code should be directly
executable.

> var arrDate = oDate.value.split("/");
> var useDate = new Date(arrDate[2], arrDate[1]-1, arrDate[0]); //
>yyyy/mm/dd

or
        var useDate = // via yyyy/mm/dd
          new Date(oDate.value.replace(/(..).(..).(....)/, "$3/$2/$1"))

> var today = new Date();

> if (useDate <= today)
> {
> args.IsValid = true;
> }
> else
> {
> args.IsValid = false;
> }

Should be args.IsValid = useDate <= today

>}

>function doDateCheckDod(dodTxtBx, dobLbl, args)
>{
> // Convert DOD to javascript date format
> var oDodDate = dodTxtBx; // dd/mm/yyyy
> var arrDodDate = oDodDate.value.split("/");
> var useDodDate = new Date(arrDodDate[2], arrDodDate[1],
>arrDodDate[0]); // yyyy/mm/dd
>
> // Convert DOB to javascript date format
> var oDobDate = dobLbl; // dd/mm/yyyy
> var arrDobDate = oDobDate.value.split("/");
> var useDobDate = new Date(arrDobDate[2], arrDobDate[1],
>arrDobDate[0]); // yyyy/mm/dd

Repeated code should generally be put in a function. When that is done,
and the function is small, temporary local variables do not need long
names.
 

-- 
 © John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   IE 4 ©
 <URL:http://www.jibbering.com/faq/>  JL/RC: FAQ of 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.
Received on Mon May 1 05:26:16 2006