我想知道是否有人能帮忙。我有一个脚本,从电子表格列表中提取数据,这是本周的匹配(基本上是一个事件列表,用于生成每周议程)。我将使用for循环来增加添加的天数,但我只是想让它工作一天.
第一列是dd/mm/yyy格式的数据
我试图将今天的增量取1,然后搜索列表以找到匹配项。搜索等,我可以做工作,但日期部分只是不玩。我想知道有没有人能给我建议。
例如,日期栏A: 06/07/2021 01/2021 01/11/20 21 01/11/20 21 01/11/20 21 02/09/2021 02/09/2021 02/09/2021
var selectedDate = row[0];
selectedDate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var currdate = new Date();
currdate = Utilities.formatDate(new Date(selectedDate), "GMT+1", "dd/MM/yyyy");
var daystochange = 1;
var newdate = new Date(currdate.getFullYear, currdate.getMonth, currdate.getDay+daystochange );有人能帮忙吗?
谢谢
发布于 2021-07-05 10:25:10
只使用Utilities.formatDate()输出日期,而不使用日期。
JavaScript date对象拥有处理日期和比较所需的全部内容。当您使用Utilities函数时,它会将其转换为字符串,因此您将失去Date对象的所有功能。
还请记住,如果您的工作表中有格式化为日期的日期,则它们将自动作为Date对象返回。
例如,如果工作表在单元格A1中有日期
var date = Sheet.getRange("A1").getValue()
date instanceof Date // true一旦你有了约会,如果你想增加一天的时间,你可以采取一种与你已经做过的类似的方法:
var selectedDate = new Date(2021, 1, 15)
var newdate = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate() + 1);
console.log(newdate) // Tue Feb 02 2021 00:00:00注-使用getDate返回月中的一天,getDay只返回一周中的一天。
要检查两个日期是否相同,可以编写一个函数进行比较:
function isSameDate(a, b) {
return a instanceof Date &&
b instanceof Date &&
a.getYear() === b.getYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
}如果日期相同,此函数将返回true。
参考文献
https://stackoverflow.com/questions/68253219
复制相似问题