Javascript: new date, missing year

I named the new date (January 4) and the default year found was 2001.

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

Is there a way that I can set the default year in 2011?


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

However, I understand that if you make Date ("January 4, 2008 2011"), the year will be 2008, but not 2011. This is what I need. In any case, I will answer the first answer.

+4
source share
2 answers

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 
+1
source

This is truly strange behavior. This is similar to Chrome. Firefox returns an invalid date. I think 2001 will be cool forever.

+1
source

All Articles