Javascript date.getYear()在2011年返回111? [英] Javascript date.getYear() returns 111 in 2011?

查看:98
本文介绍了Javascript date.getYear()在2011年返回111?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(文件).ready(function(){
$(#DateFrom)。datepicker({dateFormat:'dd / mm / yy'});
$(#DateTo)。datepicker {dateFormat:'dd / mm / yy'});

var now = new Date();
var firstDayPrevMonth = new Date(now.getYear(),now.getMonth - 1,1);
var firstDayThisMonth = new Date(now.getYear(),now.getMonth(),1);
var lastDayPrevMonth = new Date(firstDayThisMonth - 1);

$(#DateFrom)。datepicker(setDate,firstDayPrevMonth);
$(#DateTo)。datepicker(setDate,lastDayPrevMonth);
});

BUT now.getYear()正在返回111而不是预期的。有没有什么明显的我错过了?

解决方案

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getYear


getYear 不再使用,已被 getFullYear 方法



getYear 方法返回年份减去1900;因此:




  • 对于大于或等于2000的年份, getYear返回的值 100以上。例如,如果年份是2026年, getYear 返回126.

  • 在1900年和1999年之间的年份之间,返回的值 getYear 在0到99之间。例如,如果年份是1976, getYear 返回76。

  • 在1900年以下的年份, getYear 返回的值小于0.例如,如果年份为1800,则 返回-100。

  • 为了考虑2000年以前和之后的年龄,您应该使用 getFullYear 而不是 getYear ,以便年份完整指定。



I have this javascript for automatically setting a date filter to the first and last day of the previous month:

$(document).ready(function () {
    $("#DateFrom").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#DateTo").datepicker({ dateFormat: 'dd/mm/yy' });

    var now = new Date();
    var firstDayPrevMonth = new Date(now.getYear(), now.getMonth() - 1, 1);
    var firstDayThisMonth = new Date(now.getYear(), now.getMonth(), 1);
    var lastDayPrevMonth = new Date(firstDayThisMonth - 1);

    $("#DateFrom").datepicker("setDate", firstDayPrevMonth);
    $("#DateTo").datepicker("setDate", lastDayPrevMonth);
}); 

BUT now.getYear() is returning 111 instead of the expected 2011. Is there something obvious I've missed?

解决方案

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getYear

getYear is no longer used and has been replaced by the getFullYear method.

The getYear method returns the year minus 1900; thus:

  • For years greater than or equal to 2000, the value returned by getYear is 100 or greater. For example, if the year is 2026, getYear returns 126.
  • For years between and including 1900 and 1999, the value returned by getYear is between 0 and 99. For example, if the year is 1976, getYear returns 76.
  • For years less than 1900, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100.
  • To take into account years before and after 2000, you should use getFullYear instead of getYear so that the year is specified in full.

这篇关于Javascript date.getYear()在2011年返回111?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆