|
|
|
date: Mon, 16 Jun 2008 16:52:19 -0700,
group: microsoft.public.inetsdk.programming.scripting.jscript
back
Re: Validate Date data
Ron Hinds wrote:
> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
> date, Date.parse returns a valid integer (1214895600000).
From ECMA-262 (JavaScript standard):
15.9.4.2 ... in general, the value produced by Date.parse is
implementation-dependent when given any string value that could not be
produced in that implementation by the toString or toUTCString method.
In other words, the behavior of Date.parse is undefined when given an
invalid string. I suspect that in IE's implementation it "fixes up" the
date the same way Date constructor and Date.setDay et al do (try
executing alert(new Date(2008,5,31).toString()) in your browser; 5
stands for June since months are 0-based in Date's constructor).
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
date: Mon, 16 Jun 2008 20:26:20 -0400
author: Igor Tandetnik
Re: Validate Date data
"Igor Tandetnik" wrote in message
news:eDxYFDB0IHA.4912@TK2MSFTNGP03.phx.gbl...
> Ron Hinds wrote:
>> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
>> date, Date.parse returns a valid integer (1214895600000).
>
> From ECMA-262 (JavaScript standard):
>
> 15.9.4.2 ... in general, the value produced by Date.parse is
> implementation-dependent when given any string value that could not be
> produced in that implementation by the toString or toUTCString method.
>
>
> In other words, the behavior of Date.parse is undefined when given an
> invalid string. I suspect that in IE's implementation it "fixes up" the
> date the same way Date constructor and Date.setDay et al do (try executing
> alert(new Date(2008,5,31).toString()) in your browser; 5 stands for
> June since months are 0-based in Date's constructor).
That's interesting - it comes back with July 1, 2008 - which is what I
suspected. But, it doesn't solve my problem - i.e, how do I make sure a date
is valid in JScript?
date: Tue, 17 Jun 2008 11:21:43 -0700
author: Ron Hinds
Re: Validate Date data
"Ron Hinds" wrote in message
news:uaQ5$bK0IHA.4848@TK2MSFTNGP05.phx.gbl...
> "Igor Tandetnik" wrote in message
> news:eDxYFDB0IHA.4912@TK2MSFTNGP03.phx.gbl...
>> Ron Hinds wrote:
>>> The problem is, when I input e.g. "6/31/2008", which is NOT a valid
>>> date, Date.parse returns a valid integer (1214895600000).
>>
>> From ECMA-262 (JavaScript standard):
>>
>> 15.9.4.2 ... in general, the value produced by Date.parse is
>> implementation-dependent when given any string value that could not be
>> produced in that implementation by the toString or toUTCString method.
>>
>>
>> In other words, the behavior of Date.parse is undefined when given an
>> invalid string. I suspect that in IE's implementation it "fixes up" the
>> date the same way Date constructor and Date.setDay et al do (try
>> executing alert(new Date(2008,5,31).toString()) in your browser; 5
>> stands for June since months are 0-based in Date's constructor).
>
> That's interesting - it comes back with July 1, 2008 - which is what I
> suspected. But, it doesn't solve my problem - i.e, how do I make sure a
> date is valid in JScript?
>
Here's one I use:
function isValidDate(year, month, day)
{
try
{
var dt = new Date(year, month, day);
if (isNaN(dt))
{
return false;
}
var iYear2 = dt.getFullYear();
var iMonth2 = dt.getMonth();
var iDate2 = dt.getDate();
if (iYear2 != year ||
iMonth2 != month ||
iDate2 != day)
{
return false;
}
}
catch(e)
{
return false;
}
return true;
}
I first split the actual string, usually on the / character and pass the
resultant three parts to the function after taking 1 from the month.
If there aren't three parts it fails.
var iParts = dateString.split("/");
if i{Parts.length != 3) return false;
return isValidDate(iPart[2], iPart[1] - 1, iPart[0]);
Wrapped in a try/catch that returns false on an error.
--
Joe Fawcett (MVP - XML)
http://joe.fawcett.name
date: Wed, 18 Jun 2008 09:32:16 +0100
author: Joe Fawcett am
|
|