RegExp for Date

Z

zzzxtreme

hello i have this function to check date (not mine)


function (s_date) {
// check format
if (!re_dt.test(s_date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1), RegExp.$1);
if (dt_test.getMonth() != Number(RegExp.$2-1))
return false;
return true;
}


it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?
 
R

RobG

hello i have this function to check date (not mine)


function (s_date) {
// check format
if (!re_dt.test(s_date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1), RegExp.$1);
if (dt_test.getMonth() != Number(RegExp.$2-1))
return false;
return true;
}


it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?

How are we to know, you haven't shown what the actual regular exression
is. Presumably there is something somwhere that gives 're_dt' a value?
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Fri, 27 May 2005 03:17:52, seen in (e-mail address removed) posted :
hello i have this function to check date (not mine)


function (s_date) {
// check format
if (!re_dt.test(s_date))
return false;
// check allowed ranges
if (RegExp.$1 > 31 || RegExp.$2 > 12)
return false;
// check number of day in month
var dt_test = new Date(RegExp.$3, Number(RegExp.$2-1),
RegExp.$1);
if (dt_test.getMonth() != Number(RegExp.$2-1))
return false;
return true;
}


it will check in this format DD-MM-YYYY
is it possible to modify it to be (MM/DD/YYYY OR DD/MM/YYYY) in one
function?

Yes. But neither DMY not MDY is good; YMD is how dates should be,
especially in data.

You should NOT in one input allow either DMY or MDY to be valid. One
can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
apart - but is 01-07-2006, which is certainly valid, in Winter or in
Summer?

Therefore, your proposed modification would be too unwise to support.


There is no point in checking > 31 and > 12 as such; and you would, if
there were, also need to consider <=0.

The first 'return false' is probably unnecessary; if the final check is
judicious, it will catch soon enough what the first one would. But it
could be made to give a message "that does not even look like a date".

In js-date4.htm#DVal :-


function ValidDate(y, m, d) { // m = 0..11 ; y m d integers, y!=0
with (new Date(y, m, d))
return (getMonth()==m && getDate()==d) /* was y, m */ }


Read the newsgroup FAQ, especially sections 2.3 and 3.2.p (& 4.30).
<FAQENTRY> Could Sec 3.2 become an <ol type=a> list, please </FAQENTRY>
See below.
 
R

RobG

Dr John Stockton wrote:
[...]
You should NOT in one input allow either DMY or MDY to be valid. One
can see that 25.12.2004 and 12/25/2005 are a year (and about 5000 miles)
apart - but is 01-07-2006, which is certainly valid, in Winter or in
Summer?

For me at least, 01-07-2006 will be winter (global warming ignored).
It will also likely be winter in that place that persists with M-D-Y
format, but 6 months earlier. :)

Of course those who share the same hemisphere will disagree.

[...]
 

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
473,781
Messages
2,569,619
Members
45,316
Latest member
naturesElixirCBDGummies

Latest Threads

Top