validate date object; how?

O

okey

The following will not, I assume, give me a proper date object ready
for use.

var oD = new Date("garbage");

I tried to recognize a bad one with

if ( ! oD ) { ....

and a try / catch

try {
oD = new Date("garbage");
} catch ....

Is there a common way to validate the Date object? I can test the snot
out of the date initialization parameters, but I thought there must be
a better way. Thank you.
 
O

okey

The following will not, I assume, give me a proper date object ready
for use.

var oD = new Date("garbage");

I tried to recognize a bad one with

if ( ! oD ) { ....

and a try / catch

try {
  oD = new Date("garbage");

} catch ....

Is there a common way to validate the Date object? I can test the snot
out of the date initialization parameters, but I thought there must be
a better way.  Thank you.

what about?...

if (isNaN(oD.getDate() + oD.getMonth() + oD.getYear())) { ...

(if any getXXXX() is NaN, then all are not NaN?)

Just reaching..
 
O

okey

No need for reaching; this has been solved before.

<http://jibbering.com/faq/#posting>

PointedEars
--
    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

isInRange = year >= 0 && year <= 9999,

So the method is to insure the initialization parameters are correct.
Shucks, was
hoping for something else.

FYI for the Oracle crowd, at least for the min year

isInRange = year > 0 && year <= 9999,

Thanks for the info.
 
L

Lasse Reichstein Nielsen

okey said:
The following will not, I assume, give me a proper date object ready
for use.

Have you tried it?
What does oD.getTime() return? (I expect NaN)
var oD = new Date("garbage");

I tried to recognize a bad one with

if ( ! oD ) { ....

Won't work. The Date object, oD, is an object. All objects type convert
to boolean as true.
Try
if (isNaN(oD.getTime())) { ...
(don't try if(!oD.getTime()) {...} , since it will fail for Jan 1. 1970)
and a try / catch

try {
oD = new Date("garbage");

No exception thrown, so no go.
} catch ....

Is there a common way to validate the Date object? I can test the snot
out of the date initialization parameters, but I thought there must be
a better way. Thank you.

If the date is invalid, getTime returns NaN (as does any of the other
getter methods that depend on the time)

/L
 
D

Dr J R Stockton

In comp.lang.javascript message <c59e890c-8b5a-4839-b48b-a3f63084d97c@r3
4g2000vba.googlegroups.com>, Thu, 6 Aug 2009 11:25:39, okey

It gives a valid Date Object, with value NaN, "A Date object contains a
number indicating a particular instant in time to within a millisecond.
The number may also be NaN, indicating that the Date object does not
represent a specific instant of time."

A Date Object is invariably valid. Any attempt to create or adjust a
Date Object will lead to one containing either the value NaN or a value
in the range -864e13 to +864e13 inclusive (except that in Safari higher
numbers are, although sinful, possible [1]).

If you had had the sagacity to read the newsgroup FAQ, linked daily from
obvious articles here, you might have come across its Section 4.2; or a
Web search for "JavaScript date string validate" would have shown over
two million candidates, at least one of which should be good for numeric
dates.

Your proposed test will necessarily fail to reject, in at some browsers,
bad month names. One accepts "Bananas" as meaning the current month;
two others accept "Octopus" as meaning October. See
<URL:http://www.merlyn.demon.co.uk/js-datex.htm> for that, and
<URL:http://www.merlyn.demon.co.uk/js-date4.htm> for validation.


Since some common date formats, such as 07/08/09, have multiple
interpretations, one needs to stipulate the format.

what about?...

if (isNaN(oD.getDate() + oD.getMonth() + oD.getYear())) { ...

(if any getXXXX() is NaN, then all are not NaN?)

Just reaching..
t


[1] ISTM that Safari uses 32-bit signed years internally.
 
T

Thomas 'PointedEars' Lahn

okey said:
Thomas said:
okey said:
Just reaching..
No need for reaching; this has been solved before.

<http://jibbering.com/faq/#posting>
[...]

isInRange = year >= 0 && year <= 9999,

So the method is to insure the initialization parameters are correct.
Shucks, was
hoping for something else.

FYI for the Oracle crowd, at least for the min year

isInRange = year > 0 && year <= 9999,

What led you to believe that any of this would be anywhere near correct?
Which "Oracle crowd" are you talking about?
Thanks for the info.

What "info"?

Learn to quote.


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <e820b4f8-e3b2-4be8-91b8-45053804137c@q1
4g2000vbi.googlegroups.com>, Thu, 6 Aug 2009 13:58:51, okey

Please don't quote signatures; read the FAQ.
isInRange = year >= 0 && year <= 9999,

So the method is to insure the initialization parameters are correct.
******

One insures by paying money to an insurance company. By being
knowledgeable and careful, one can (often) ensure.
Shucks, was
hoping for something else.

FYI for the Oracle crowd, at least for the min year

isInRange = year > 0 && year <= 9999,

Thanks for the info.


Please remember that Thomas Lahn will never give a useful answer if
there is any alternative.
 
D

Dr J R Stockton

Thu said:
Try
if (isNaN(oD.getTime())) { ...
(don't try if(!oD.getTime()) {...} , since it will fail for Jan 1. 1970)

It does not fail for 01/01/70 in America. In fact, it fails for a
single millisecond, 1970-01-01 00:00:00 GMT. For most programmers and
applications, that instant is ancient history.


If the date is invalid, getTime returns NaN (as does any of the other
getter methods that depend on the time)

To get NaN, the Date Object must have been set with something that it
could not contrive to interpret as a valid date. Firefox, for one,
gives, for new Date("66/66/9999") , an Object representing
Thu Aug 05 10004 00:00:00 LCT. Other browsers accept even sillier
strings.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Please remember that Thomas Lahn will never give a useful answer if
there is any alternative.

Only a complete idiot (or someone who has not been reading this NG for more
than a day) would say so.


PointedEars
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top