当我尝试添加接近 11 月 30 日(任何年份)的日期时,我的代码表现得很有趣:
Date.prototype.addDays = function(days){
this.setDate(this.getDate() + days);
return this;
};
function calculateDate(string_date, days_to_add){
var arr, dat;
arr = string_date.split(" ");
dat = new Date(2013, (("enefebmarabrmayjunjulagosepoctnovdic".indexOf(arr[1])+3)/3), (((arr[0].charAt(0)!="0")?arr[0]:arr[0].substring(1))*1));
dat.addDays(days_to_add*1);
return (dat.getDate() + "/" + dat.getMonth() + "/"+dat.getFullYear());
}
现在如果我使用:
calculateDate("07 nov",24);
返回31/11/2013
(我的日历显示 2013 年 11 月的日期为 30)calculateDate("07 nov",25);
返回1/0/2014
我的代码似乎适用于任何其他月份和日期,那么为什么我的代码在 11 月至 12 月附近无法正常工作?计算机是否已经产生了感情并需要休假才能继续工作?
最佳答案
Date
对象 getMonth
是一个从零开始的索引:
注意到“11 月”有 31 天吗?
关于javascript - 为什么我的 js 日期在 11 月份左右开始计算错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17560364/