我試圖將明天的日期與通過谷歌表單提交的日期相匹配。在記錄器中它似乎匹配,但它不會記錄 YES 并且不會評估為真。
我的努力:
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)
}}}
uj5u.com熱心網友回復:
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();
}
這是一個選擇一天內發生的時間戳的小例子:
虛假資料:
| 時間戳 | 日型 |
|---|---|
| 10/11/2021 0:00:00 | 昨天 |
| 10/11/2021 12:00:00 | 昨天 |
| 10/12/2021 0:00:00 | 一天的開始 |
| 10/12/2021 12:00:00 | 今天 |
| 10/13/2021 0:00:00 | 明天 |
| 10/13/2021 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/313662.html
標籤:javascript 约会时间 谷歌应用程序脚本 谷歌表格 日期比较
上一篇:在組件中的BlazorWebAssembly中,假設文化正確,如何使DateTime.ToString使用當前文化的日期格式?
下一篇:創建一個日期時間添加月份的列
