cheng
cheng

Reputation: 6696

javascript: new date, missing year

I called new Date("Jan 4") and found the default year is 2001

a=new Date("Jan 4")
Thu Jan 04 2001 00:00:00 GMT-0500 (EST)

Is there any way that I can set the default year to be 2011?


update: I know that I can use + 2011 but since I have some "Jan 4", and some "Jan 4, 2008" I only want to use 2011 for the former case.

However, I realize that if you do Date("Jan 4, 2008 2011") the year will be 2008 but not 2011. That's just what I need. I will mark the first answer correct anyway.

Upvotes: 5

Views: 807

Answers (2)

Neal Ehardt
Neal Ehardt

Reputation: 10954

This is truly bizarre behavior. It seems to be specific to Chrome. Firefox returns an invalid date. I guess 2001 will be cool forever.

Upvotes: 2

ChristopheCVB
ChristopheCVB

Reputation: 7315

Can you try :

year = new Date().getFullYear(); // current year
a=new Date("Jan 4"+' '+year);

Update

You can split the string using

'Jan 4, 2008'.split(',')[0]+' '+year;
'Jan 4'.split(',')[0]+' '+year; // still works

Upvotes: 1

Related Questions