[英]date object not as expected using setDate
var start_date = new Date(ss.getSheetByName('3D Tracker').getRange('I119').getValue());
使用调试工具的开始日期计算为Wed Oct 01 2014 00:00:00 GMT-0400 (EDT)
我想创建一个新变量,即start plus 10 days。
var start = start_date;
var end = new Date(start.setDate(start.getDate() + 10));
记录器的输出为:
start: Sat Oct 11 2014 00:00:00 GMT-0400 (EDT)
end: Sat Oct 11 2014 00:00:00 GMT-0400 (EDT)
我预计将从Wed Oct 01 2014 00:00:00 GMT-0400
并在Sat Oct 11 2014 00:00:00 GMT-0400 (EDT)
。 不知何故,我的脚本开始和结束都增加了10天,而我只想结束了10天。
我想念什么吗? 环境是基于Javascript的Google Apps脚本。
setDate()
在返回计算出的值之前修改首先调用的Date
对象。 因此,您要在从更新的时间戳记start
创建end
10天之前start
。
如果要使用setDate()
而不专门更改start
,则需要首先从中创建new Date()
。
var start = new Date(ss.getSheetByName('3D Tracker').getRange('I119').getValue());
var end = new Date(start.getValue()); // initially match `start`
end.setDate(end.getDate() + 10); // change to 10 days later
对于单行代码,您还可以计算新的时间戳记,其中10 * 24 * 60 * 60 * 1000
等于10天的毫秒数:
var end = new Date(start.getValue() + 10 * 24 * 60 * 60 * 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.