最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - javascript date and year - Stack Overflow

programmeradmin4浏览0评论

I have the following javascript code:

<script type="text/javascript">
    $(function () {
        var currentDateTime = new Date();
        var oneYear = new Date();
        oneYear.setYear(oneYear.getYear() + 1);
        alert(currentDateTime + "_" + oneYear);
    });
</script>

i would expect the alert to output the current datetime and the datetime of one year from now. However I get this in the alert: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)"

Clearly it's not adding "1" to the Year correctly!

Whats going on? How did it bee the year 0111???

I have the following javascript code:

<script type="text/javascript">
    $(function () {
        var currentDateTime = new Date();
        var oneYear = new Date();
        oneYear.setYear(oneYear.getYear() + 1);
        alert(currentDateTime + "_" + oneYear);
    });
</script>

i would expect the alert to output the current datetime and the datetime of one year from now. However I get this in the alert: "Fri Oct 22 2010 14:17:31 GMT-0400 (Eastern Daylight Time)_Thu Oct 22 0111 14:17:31 GMT-0400 (Eastern Daylight Time)"

Clearly it's not adding "1" to the Year correctly!

Whats going on? How did it bee the year 0111???

Share Improve this question asked Oct 22, 2010 at 18:18 kralco626kralco626 8,64441 gold badges115 silver badges171 bronze badges 1
  • Related (duplicate?): [ Why does Javascript getYear() return 108? ](stackoverflow./questions/98124/…) – kennytm Commented Oct 22, 2010 at 18:24
Add a ment  | 

3 Answers 3

Reset to default 14

It is correct. .getYear() returns "actual year − 1900". 2010 − 1900 = 110.

Use .getFullYear() instead. .getYear() has been deprecated for a long time.

Y2K was 10 years ago, but you're still using getYear instead of getFullYear? tsk tsk...

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Date/getFullYear

Instead of .getYear() try .getFullYear()

发布评论

评论列表(0)

  1. 暂无评论