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
3 Answers
Reset to default 5You 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
版权声明:本文标题:javascript - Why isn't my date parsing correctly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744887882a2630596.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论