JK.*_*JK. 163 javascript datepicker
我有这个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);
});
Run Code Online (Sandbox Code Playgroud)
但是now.getYear()返回111而不是预期的2011年.有什么明显的我错过了吗?
dec*_*eze 281
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getYear
getYear已不再使用,已被该getFullYear方法取代.该
getYear方法返回年份减去1900年; 从而:
- 对于大于或等于2000的年份,返回的
getYear值为100或更大.例如,如果年份是2026年,则getYear返回126.- 在1900年和1999年之间
getYear的年份,getYear返回的值在0到99之间.例如,如果年份是1976年,则返回76.- 对于少于1900年的年份,返回的值
getYear小于0.例如,如果年份为1800,则getYear返回-100.- 考虑到2000年之前和之后的年份,您应该使用
getFullYear而不是getYear使年份全部指定.
SLa*_*aks 31
为了遵守愚蠢的先例,getYear()返回自1900年以来的年数.
相反,你应该打电话getFullYear(),它返回实际的年份.
根据我在Mozilla的JS页面上看到的内容,不推荐使用getYear.正如多次指出的那样,getFullYear()是要走的路.如果你真的想要使用getYear()添加1900.
var now = new Date(),
year = now.getYear() + 1900;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101166 次 |
| 最近记录: |