我正在研究原生的 Date 对象,我意识到减法是有效的(返回一个整数,表示两个日期之间的总毫秒数),但加法只是通过字符串连接将它们组合起来。同样,向日期添加一个整数(例如我想获取 3000 毫秒后的时间)将返回一个字符串,而向日期减去一个整数则有效。
只是想知道这是否是预期的行为以及我是否遗漏了什么?
最佳答案
您只需将当前日期转换为毫秒,添加所需的增量,然后将其转换回日期时间格式,即可获得增量日期:
var d = new Date();
alert("Current DateTime: " + d);
var milliseconds = d.getTime(); //this will convert current date into milliseconds..
//Now youw want to progress the date by 3000ms.. simply add it to the current date time..
milliseconds += 3000;
d = new Date(milliseconds); //your new incremented date
alert("After 3000ms: " + d);
关于javascript - Date 对象的整数加/减问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25800862/