javascript - 谷歌脚本将明天的日期与谷歌表单日期输入匹配
问题描述
我试图将明天的日期与通过谷歌表单提交的日期相匹配。在记录器中它似乎匹配,但它不会记录是并且不会评估为真。
我的努力:
function ArchiveTuesdayOrder() {
let dt = new Date();
let t = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1);//default for items not provided in the constructor is zero
const date = new Date();
date.setDate(date.getDate() + 1);
var tom = Utilities.formatDate(date, "America/New_York", 'EEE MMM dd yyyy HH:mm:ss Z');
var tomDate = Utilities.formatDate(date, "America/New_York", "MM-dd-yyyy");
var sheetActive = SpreadsheetApp.openById("xxxxxxxxxxxxxx");
var sheet = sheetActive.getSheetByName("xxxxxxxxxxxxxx");
//var orderDate = sheet.getRange(2,3,100).getValues();
var orderdateRange = sheet.getRange(2, 4, 100);
var orderDate = orderdateRange.getValues();
Logger.log(tom.substring(0,10))
Logger.log(t);
for (var i = 0; i < orderDate.length; i++) {
Logger.log(orderDate[i])
if (t === orderDate[i]) { // This is what I cant get to evaluate true- No Match
Logger.log("YES"+orderDate)
}}}
解决方案
function tomorrow() {
let dt = new Date();
dt.setDate(dt.getDate() + 1);
Logger.log(dt);
return dt.valueOf();//milliseconds can be used in numerical comparisons
}
function tomorrowstartofday() {
let dt = new Date();
let t = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1);//default for items not provided in the constructor is zero
Logger.log(t);
return t.valueOf();
}
这是一个选择一天内发生的时间戳的小例子:
假数据:
时间戳 | 日型 |
---|---|
2021 年 10 月 11 日 0:00:00 | 昨天 |
2021 年 10 月 11 日 12:00:00 | 昨天 |
2021 年 10 月 12 日 0:00:00 | 一天的开始 |
2021 年 10 月 12 日 12:00:00 | 今天 |
2021 年 10 月 13 日 0:00:00 | 明天 |
2021 年 10 月 13 日 12:00:00 | 明天 |
编码:
function timestampsfortoday() {
const dt = new Date();
const tod = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//today
const tom = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() + 1).valueOf();//tomorrow
let ts = [];
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
const vs = sh.getRange(2,1,sh.getLastRow() - 1,2).getValues();
vs.forEach(r => {
let d = new Date(r[0]).valueOf();//using Date() constructor to get date value from timestamp
if(d > tod && d < tom) {
ts.push(r);
}
});
Logger.log(ts.join('\n'))
}
执行日志:
9:58:44 AM Notice Execution started
9:58:46 AM Info Tue Oct 12 2021 12:00:00 GMT-0600 (Mountain Daylight Time),today
9:58:45 AM Notice Execution completed
它只在第二列中选择了今天的行,因为这是今天开始和明天开始之间的唯一一个。
如果您在循环中使用此行进行比较:
if(d >= tod && d < tom)
你得到这个:
Execution log
10:05:58 AM Notice Execution started
10:05:59 AM Info Tue Oct 12 2021 00:00:00 GMT-0600 (Mountain Daylight Time),start of day
Tue Oct 12 2021 12:00:00 GMT-0600 (Mountain Daylight Time),today
10:05:59 AM Notice Execution completed
推荐阅读
- javascript - 绕过打印对话框在加载时自动打印
- java - 映射 UML 图到 Java 代码转换
- javascript - 字符串匹配函数返回一个数组,但如果转换为数字,则返回匹配的数字
- c++ - 我遇到了 C++“参数太多”警告的问题
- python - SQLAlchemy 有最大数量的关系?
- c++ - CPP 中的 ++ 运算符是否对 char 有任何重载?
- python - 为列表列表中的每个列表映射一个函数
- python - 如何让 Pandas 从下一行开始在新列中创建 sumsum?
- mfc - MFC CImageList DeleteImageList() vs Remove()
- android - Android studio 线性布局问题:我应该如何在没有嵌套权重的情况下布局我的?