Javascript 添加日期日期时的奇怪行为
我想在约会中增加一天,但实际上我得到了一个奇怪的结果 我以今天作为当前日期(2019年2月28日)进行测试,并尝试添加400天,我现在是2096年。。。我认为特瑞是个问题 看看我的功能:Javascript 添加日期日期时的奇怪行为,javascript,date,Javascript,Date,我想在约会中增加一天,但实际上我得到了一个奇怪的结果 我以今天作为当前日期(2019年2月28日)进行测试,并尝试添加400天,我现在是2096年。。。我认为特瑞是个问题 看看我的功能: const user={LastReport:new Date(),Validite:“413”} var temp=新日期(user.LastReport) 控制台日志(“当前:+temp”); user.DateValide=temp.setDate(temp.getDate()+user.Validit
const user={LastReport:new Date(),Validite:“413”}
var temp=新日期(user.LastReport)
控制台日志(“当前:+temp”);
user.DateValide=temp.setDate(temp.getDate()+user.Validite);
console.log(“添加日期:“+user.Validite”)
console.log(“Result:+new Date(user.DateValide))
将temp.getDate()+user.Validite
连接为字符串,因此添加的天数是28413
而不是441
使用parseInt()
将它们转换为数字,或者可以使用类似+
的算术运算符作为它们的前缀
const user={LastReport:new Date(),Validite:“413”}
var temp=新日期(user.LastReport)
控制台日志(“当前:+temp”);
//user.DateValide=temp.setDate(parseInt(temp.getDate())+parseInt(user.Validite));
user.DateValide=temp.setDate(+temp.getDate()++user.Validite);
console.log(“添加日期:“+user.Validite”)
console.log(“结果:+新日期(user.DateValide))
可能重复的。。。。。是的,我看过很多关于在约会中增加一天的话题。但实际上,我的代码看起来和其他主题一样,仍然不起作用。我已经创建了一个代码片段。似乎有效fine@adiga哈哈,它怎么能附加?seeriouslyaha413
是一个字符串。检查张贴的答案。请从下一次在ECMAScript中创建一个“+”字符,该字符重载,有三种可能的含义。当像++user.Validite
一样使用时,第一个被视为,第二个被视为。:-)