What does this do

T

timmy_dale12

Im implementing a calendar and cant figure out what this method does
or how it works

// datetime parsing and formatting routimes. modify them if you wish
other datetime format
function str2dt (str_datetime) {
var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
if (!re_date.exec(str_datetime))
return alert("Invalid Datetime format: "+ str_datetime);
return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4,
RegExp.$5, RegExp.$6));
}

Mostly because of this expression RegEx.$ , which ive never seen
before. What does it do
 
L

Lasse Reichstein Nielsen

Mostly because of this expression RegEx.$ , which ive never seen
before. What does it do

RegExp.$<n> gives you the n'th partial match of the last regualar
expression match.
So, if you match
/a(.*)a(.*)a/.exec("axxxayyyya")
then RegExp.$1 == "xxx" and RegExp.$2 == "yyyy".

I would prefer to use the return value of exec instead:
if ((match = re.exec(string)) {
...match[1]... match[2]...
}

Here match[1] refers to the same as RegExp.$1, but it is not destroyed
by a later regular expression match.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
in news:comp.lang.javascript, (e-mail address removed) posted at Thu, 18
Sep 2003 01:30:48 :-
Im implementing a calendar and cant figure out what this method does
or how it works

// datetime parsing and formatting routimes. modify them if you wish
other datetime format
function str2dt (str_datetime) {
var re_date = /^(\d+)\-(\d+)\-(\d+)\s+(\d+)\:(\d+)\:(\d+)$/;
if (!re_date.exec(str_datetime))
return alert("Invalid Datetime format: "+ str_datetime);
return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4,
RegExp.$5, RegExp.$6));

In addition to what Lasse wrote : the function returns a Date Object set
to the browser's-locality date/time in the string, assuming that to be
of the form "D-M-Y h:m:s" where DMYhms are any number >0 of digits and
the date and time are separated by any amount of whitespace. If the
given year is in 00..99, 1900 will be added.

With whatever error there may be in the OS setting of Time Zone and
Summer Time rules; depending on the application, consequences may be
non-obvious or intermittent.

Twice, \ before - is not needed in the RegExp.

Any number can over/under-flow its field, so 31-9-2003 => 1-10-2003 and
25-0-2004 is this Christmas; in other words, there is no value
validation, only a pattern test. See via below.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top