admin管理员组

文章数量:1405393

I'm having issues parsing dates after 01/01/2000. The results are being returned incorrectly. 1999 is getting parsed as the year 1999, when it gets to 2000 it is parsing it as 0100, and then 2001 as 0101, etc. Here is the test code to illustrate this issue:

<script type="text/javascript" language="javascript">


// functions incorrect  changes year from 2010 to 0101
var d = (new Date("12/01/2009"));
if (d.getMonth() < 11) 
 { d = new Date(d.getYear(), d.getMonth() + 1, 1); } 
 else
 { d = new Date(d.getYear() + 1, 0, 1); } 
document.write(d);
//  Result:  Sat Jan 01 0101 00:00:00 GMT-0500 (Eastern Standard Time) 

document.write('<br />');


document.write(Date.parse(Date()) < Date.parse(d));
// 
// Result: false  today should definitely be < 01/01/2010


document.write('<br />');


// Functions correctly if year is before 2000

var d = (new Date("12/01/1998"));

if (d.getMonth() < 11) 
 { d = new Date(d.getYear(), d.getMonth() + 1, 1); } 
 else
 { d = new Date(d.getYear() + 1, 0, 1); } 


document.write(d);
//  Result:  Fri Jan 01 1999 00:00:00 GMT-0500 (Eastern Standard Time)  
document.write('<br />');


document.write(Date.parse(Date()) < Date.parse(d));
// false


</script>

I'm having issues parsing dates after 01/01/2000. The results are being returned incorrectly. 1999 is getting parsed as the year 1999, when it gets to 2000 it is parsing it as 0100, and then 2001 as 0101, etc. Here is the test code to illustrate this issue:

<script type="text/javascript" language="javascript">


// functions incorrect  changes year from 2010 to 0101
var d = (new Date("12/01/2009"));
if (d.getMonth() < 11) 
 { d = new Date(d.getYear(), d.getMonth() + 1, 1); } 
 else
 { d = new Date(d.getYear() + 1, 0, 1); } 
document.write(d);
//  Result:  Sat Jan 01 0101 00:00:00 GMT-0500 (Eastern Standard Time) 

document.write('<br />');


document.write(Date.parse(Date()) < Date.parse(d));
// 
// Result: false  today should definitely be < 01/01/2010


document.write('<br />');


// Functions correctly if year is before 2000

var d = (new Date("12/01/1998"));

if (d.getMonth() < 11) 
 { d = new Date(d.getYear(), d.getMonth() + 1, 1); } 
 else
 { d = new Date(d.getYear() + 1, 0, 1); } 


document.write(d);
//  Result:  Fri Jan 01 1999 00:00:00 GMT-0500 (Eastern Standard Time)  
document.write('<br />');


document.write(Date.parse(Date()) < Date.parse(d));
// false


</script>
Share Improve this question edited Aug 23, 2011 at 20:00 Stephen 1,6172 gold badges18 silver badges44 bronze badges asked Mar 13, 2009 at 19:26 stephenbayerstephenbayer 12.4k15 gold badges66 silver badges99 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You need to use d.getFullYear() instead of d.getYear()

getYear() only gives the number of years since 1900, obviously ;)

You should be using Date.parse(). Part of why you're seeing odd results is because you use new Date("12/01/2009") up at the top and pare everything else to that. Note that Date.parse() returns a number, not a date object. Therefore, do this for your very first assignment:

var d = new Date(Date.parse("12/01/2009"));

The next part is that you need the getFullYear() function rather than just getYear(), though your problem there is only in your verification code, not in how you actually parse the date.

what getYear() returns is browser-dependent. Try d.getFullYear() instead.

A reasonable Javascript date reference: http://www.w3schools./jsref/jsref_obj_date.asp

本文标签: javascriptWhy isn39t my date parsing correctlyStack Overflow