【问题标题】:Google script matching tomorrows date with a google forms date input谷歌脚本将明天的日期与谷歌表单日期输入匹配
【发布时间】:2021-10-12 14:04:37
【问题描述】:

我已尝试将明天的日期与通过 Google 表单提交的日期匹配。在记录器中它似乎匹配,但它不会记录 YES 并且不会评估为 true。

我的努力:

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)
  }}}

【问题讨论】:

  • 我认为将日期转换为文本以与另一个日期进行比较不是一个好主意。而是使用 Date.prototype.getTime() 以毫秒为单位获取日期表示并使用它们进行比较。如果您需要进一步的帮助,1. 添加minimal reproducible example,包括示例输入数据和预期结果以及您的搜索工作的简要描述,如How to Ask 中所建议的那样。

标签: javascript datetime google-apps-script google-sheets date-comparison


【解决方案1】:
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();
}

这是一个挑选一天内发生的时间戳的小例子:

假数据:

TimeStamp day type
10/11/2021 0:00:00 yesterday
10/11/2021 12:00:00 yesterday
10/12/2021 0:00:00 start of day
10/12/2021 12:00:00 today
10/13/2021 0:00:00 tomorrow
10/13/2021 12:00:00 tomorrow

代码:

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

【讨论】:

  • 这很接近,我喜欢它的时间为 00:00,但我无法将其评估为与谷歌表格单元格中的时间戳匹配。日志显示:Wed Oct 13 00:00:00 GMT-05:00 2021 并且来自单元格的值在记录器中显示 [Wed Oct 13 00:00:00 GMT-05:00 2021]。
  • 如果您希望获取给定日期的所有时间戳,那么您希望使用 Date() 构造函数将时间戳转换为值,然后查找从今天开始到明天开始之间的日期天。
  • 这一切都取决于你想要什么。在我的情况下,我希望所有时间戳都大于 start 或 day today 而小于 start of day 的明天。
  • 在上一个示例中,我更改了比较以接受那些正好在一天开始的时间戳。关键是,如果您将其转换为日期值,您可以更好地控制选择要查看的提交内容。
  • 我承认我不想提供一个脚本,你可以直接复制而无需考虑它,因为我过去做过很多这样的事情,而你最终会得到一群用户最终什么也没学到,他们继续回来要求他们应该能够自己解决的变化,这使得在这里回答问题真的很无聊。
猜你喜欢
  • 2021-10-30
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2021-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多