Javascript date.getYear() 在 2011 年返回 111?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4754938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:56:15  来源:igfitidea点击:

Javascript date.getYear() returns 111 in 2011?

javascriptdatepicker

提问by JK.

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

我有这个 javascript 自动将日期过滤器设置为上个月的第一天和最后一天:

$(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?

但是now.getYear()返回 111 而不是预期的 2011。有什么明显的我错过了吗?

回答by deceze

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

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

getYearis no longer used and has been replaced by the getFullYearmethod.

The getYearmethod returns the year minus 1900; thus:

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

getYear不再使用,已被getFullYear方法取代。

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

  • 对于大于或等于 2000 年的年份,返回的getYear值为 100 或更大。例如,如果年份是 2026,则getYear返回 126。
  • 对于 1900 年和 1999 年之间(包括 1900 年和 1999 年),返回的值getYear介于 0 和 99 之间。例如,如果年份是 1976 年,则getYear返回 76。
  • 对于小于 1900getYear的年份,getYear返回的值小于 0。例如,如果年份为 1800,则返回 -100。
  • 要考虑 2000 年之前和之后的年份,您应该使用getFullYear代替,getYear以便完整指定年份。

回答by SLaks

In order to comply with boneheaded precedent, getYear()returns the number of years since 1900.

为了遵守笨蛋的先例,getYear()返回自 1900 年以来的年数。

Instead, you should call getFullYear(), which returns the actual year.

相反,您应该调用getFullYear(),它返回实际年份。

回答by Pazuzu156

From what I've read on Mozilla's JS pages, getYear is deprecated. As pointed out many times, getFullYear()is the way to go. If you're really wanting to use getYear()add 1900 to it.

根据我在 Mozilla 的 JS 页面上阅读的内容,不推荐使用 getYear。正如多次指出的那样,getFullYear()是要走的路。如果你真的想使用getYear()添加 1900 到它。

var now = new Date(),
    year = now.getYear() + 1900;