以下代码:
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var today = new Date(2013,0,31);
var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
alert("New date is "+tomorrow.getFullYear() +", "+ tomorrow.getMonth()+", "+ tomorrow.getDate())
...输出:2014, 1, 1
(演示:http://jsfiddle.net/3pA3Q/5/)
谁能解释一下?
此外,这两个结果相同:
var today = new Date(2013,11,31);
var today = new Date(2013,12,31);
我理解“月份从 0 开始到 1 月到 11 为 12 月”,所以 new Date(2013,12,31)
应该是 2014 年,1 月 31 日
最佳答案
您将 tomorrow
初始化为今天的日期,因此在这一行 tomorrow.setDate(today.getDate() + 1);
中,您只需将 1 天添加到今天的日期。
你最好克隆你的约会对象:
var today = new Date(2013,0,31);
var tomorrow = new Date(today.getTime()); // Get a copy
tomorrow.setDate(tomorrow.getDate() + 1);
关于Javascript 日期解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21175445/