views:

67

answers:

3

Hi

I noticed Date.Parse can't handle only 2 digits dates.

Say I have this

mm/dd/yy = 7/11/20

Date parse will this it is = 7/11/1920. Can you set it to use the year two thousand? Like it's kinda weird I got the jquery u.i date picker and if you type in 7/11/20 it will figure out 2020.

So it would be nice if date.parse could keep up I rather have them both not know what is going on or both know what is going on then have one that knows and one that does not know.

+1  A: 

Not that I'm aware of. But you can always adjust the year:

YourDate="7/11/20";
DateObj=new Date(YourDate.replace(/(\d\d)$/,"20$1"));

alert(DateObj);

This code in action.

Edit: The following code will handle both full and short years:

YourDate="7/11/2020";
DateObj=new Date(YourDate.replace(/\/(\d\d)$/,"/20$1"));

alert(DateObj);

This code in action.

Gert G
That's is a good trick!
BrunoLM
What is this doing? I am kinda confused with $1 also the user could always give a full date. Then this fails. So how could it be modified?
chobo2
@BrunoLM - Thanks. :)
Gert G
the problem with this approach is if they type, say, 7/11/98, the date will be 7/11/2098, which is probably not what they meant, unless the dates they're entering are always supposed to be in the future in your particular situation. There needs to be a cutoff somewhere for two digit dates to work... where the cutoff is is arbitrary, but somewhere around 50 might make sense... But really, not accepting two-digit dates at all would be the best idea I think. :)
no
@chobo2 - The `$1` inserts the value that was found in the regex (the `(\d\d)` portion, which is two digits).
Gert G
what does \d\d default too?
chobo2
@chobo2 - There's no default. `\/(\d\d)` tries to match `/dd`, where `d` is a digit. If there's a match, it will replace the match with `/20dd`, if not, which is the case with for instance `/2015`, there will not be a replace.
Gert G
@chobo2 - Are you okay with the code?
Gert G
A: 

So your question is whether you can change the way Date.parse works so that low-numbered two-digit dates are interpreted as dates after the year 2000?

Yes, it can be done, simply shadow Date.parse with your own parse function.

// don't do this!
Date.parse = function (str) { /* your parse routine here */ }

Of course, it's generally a very bad idea to shadow properties (including 'methods' aka function properties) of host objects, because it will cause incorrect behavior in other scripts that expect those properties to work a certain way.

It's also a bad idea to use two digit dates, but that might be beyond your control. If it's not beyond your control, I'd advise to just forget 2-digit dates and use the full year value instead.

no
Just to enforce: this is very, very dangerous!!
BrunoLM
Ya I don't think I will do that lol.
chobo2
A: 

How about this?

var date = '7/11/20';
var idx = date.lastIndexOf('/') + 1;
date = date.substr(0,idx) + '20' + date.substr(idx);
var result = Date.parse(date);​
alert(result);

or this version that will test for a YYYY format first.

var date = '7/11/2020'; 
var idx = date.lastIndexOf('/') + 1;
if(date.substr(idx).length < 4) {
    date = date.substr(0,idx) + '20' + date.substr(idx);
}
var result = Date.parse(date);
alert(new Date(result))​;​
RightSaidFred
Doesn't work very well you know... `7/11/2000 = 7/11/202000`
BrunoLM
@BrunoLM - I think the assumption is that YY format will always be used. Easy enough to add a test for a YYYY format.
RightSaidFred
Well a user could type in YY or YYYY. I am not sure about YYY don't know if that works. I know the first 2 C# can figure out and parse and so can datepicker.
chobo2