Google格式的日期与格式比较在Google Apps脚本中落后1天 [英] Date comparisons with formatDate in Google Apps Script are 1 day behind

查看:100
本文介绍了Google格式的日期与格式比较在Google Apps脚本中落后1天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个Google Apps脚本,该脚本通过Google Tasks API(作为一项高级服务)查看任务,并检查其到期日期以查看是否过期。如果是,则应将其移至当天。

Writing a Google Apps Script that looks at tasks via the Google Tasks API (as an Advanced Service) and checks their due date to see if they are overdue. If they are, it should then move them to the current day.

我使用了此代码创建此项目以使Google任务保持最新状态:

I used this code to create this project for keeping Google Tasks up to date:

保持Google任务更新

当将当前日期与任务到期日进行比较时,任务到期日总是滞后一天。

When comparing the current day with the date the task is due, the date the task is due is always one day behind.

查看它,我发现它可能与夏时制和短短一个小时有关关闭可能意味着两天之内的差异。我发现这个问题也有类似的问题,他们的解决方案是使用 Session.getScriptTimeZone()正确设置时区。

Looking into it I found that it could have to do with Daylight Savings Time and just one hour off could mean the difference in two days. I found this question that had a similar issue, their solution was to use Session.getScriptTimeZone() to set the timezone correctly.

但是即使我设置了使用 Session.getScriptTimeZone()时区,它仍然比任务的截止日期晚一天。

However even when I set the timezone using Session.getScriptTimeZone() it still has the task's due date one day behind.

function checkOverdue(t) {
  var today = Utilities.formatDate(new Date(), "EST", "MM/dd/yyyy");
  //var today = new Date();
  var tasks = t;
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];
      if (task.due) {
        var taskDue = Utilities.formatDate(new Date(task.due), "EST", "MM/dd/yyyy");
        Logger.log('"%s", Comparing "%s" to "%s"',
                 task.title, taskDue, today);
        if (taskDue.valueOf() < today.valueOf()) {
          Logger.log('Task with title "%s" is past due', task.title);
        }
      }
    }
  } else {
    Logger.log('No tasks found.');
  }
}



日志



Logs

[18-04-10 13:12:33:927 EDT] "Create Bio presentation", Comparing "04/09/2018" to "04/10/2018"
[18-04-10 13:12:33:928 EDT] Task with title "Create Bio presentation" is past due
[18-04-10 13:12:33:929 EDT] "Bio Presentation", Comparing "04/10/2018" to "04/10/2018"
[18-04-10 13:12:33:930 EDT] "Matrix HW", Comparing "04/09/2018" to "04/10/2018"
[18-04-10 13:12:33:930 EDT] Task with title "Matrix HW" is past due






=== [更新] ===



我注意到,如果我记录原始的 task.due ,它的日期似乎正确。因此,有关格式的某些事情正在更改日期。


=== [ UPDATE ] ===

I noticed that if I log the original task.due it appears to have the correct date. So something about the formatting is changing the date.

function checkOverdue(t) {
  var today = Utilities.formatDate(new Date(), "EST", "MM/dd/yyyy");
  //var today = new Date();
  var tasks = t;
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];
      if (task.due) {
        var taskDue = Utilities.formatDate(new Date(task.due), "EST", "MM/dd/yyyy");
        Logger.log('"%s", Comparing TaskDue: "%s" to today: "%s", task.due "%s"', task.title, taskDue, today, task.due);
        if (taskDue.valueOf() < today.valueOf()) {
          Logger.log('Task with title "%s" is past due', task.title);
        }
      }
    }
  } else {
    Logger.log('No tasks found.');
  }
}



新日志



New Logs

[18-04-10 14:15:17:628 EDT] "Create Bio presentation", Comparing TaskDue: "04/09/2018" to today: "04/10/2018", task.due "2018-04-10T00:00:00.000Z"
[18-04-10 14:15:17:629 EDT] Task with title "Create Bio presentation" is past due
[18-04-10 14:15:17:630 EDT] "Bio Presentation", Comparing TaskDue: "04/10/2018" to today: "04/10/2018", task.due "2018-04-11T00:00:00.000Z"
[18-04-10 14:15:17:631 EDT] "Matrix HW", Comparing TaskDue: "04/09/2018" to today: "04/10/2018", task.due "2018-04-10T00:00:00.000Z"
[18-04-10 14:15:17:631 EDT] Task with title "Matrix HW" is past due






=== [更新2] == =



将两个 EST 的formatDate都更改为 Session.getScriptTimeZone()


=== [ Update 2 ] ===

Changed both "EST" in formatDate to Session.getScriptTimeZone()

已删除 .valueOf()以进行比较。

仍然会导致同一问题

function checkOverdue(t) {
  var today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");
  //var today = new Date();
  var tasks = t;
  if (tasks.items) {
    for (var i = 0; i < tasks.items.length; i++) {
      var task = tasks.items[i];
      if (task.due) {
        var taskDue = Utilities.formatDate(new Date(task.due), Session.getScriptTimeZone(), "MM/dd/yyyy");
        Logger.log('"%s", Comparing TaskDue: "%s" to today: "%s", task.due "%s"', task.title, taskDue, today, task.due);
        if (taskDue < today) {
          Logger.log('Task with title "%s" is past due', task.title);
        }
      }
    }
  } else {
    Logger.log('No tasks found.');
  }
}



新日志



New Logs

[18-04-10 15:21:01:988 EDT] "Create Bio presentation", Comparing TaskDue: "04/09/2018" to today: "04/10/2018", task.due "2018-04-10T00:00:00.000Z"
[18-04-10 15:21:01:988 EDT] Task with title "Create Bio presentation" is past due
[18-04-10 15:21:01:990 EDT] "Bio Presentation", Comparing TaskDue: "04/10/2018" to today: "04/10/2018", task.due "2018-04-11T00:00:00.000Z"
[18-04-10 15:21:01:991 EDT] "Matrix HW", Comparing TaskDue: "04/09/2018" to today: "04/10/2018", task.due "2018-04-10T00:00:00.000Z"
[18-04-10 15:21:01:991 EDT] Task with title "Matrix HW" is past due


推荐答案

根据Google Tasks API文档, task#due 属性 RFC 3339 日期时间值,例如 2018-04-11T0:45:26.000Z
从此字符串中,您可以原生构建Javascript Date ,即

Per Google Tasks API documentation, the task#due property is an RFC 3339 datetime value, e.g. "2018-04-11T0:45:26.000Z". From this string, you can construct a Javascript Date natively, i.e.

var taskDueDate = new Date(task.due);

请注意,这等效于调用 Date.parse() ,因此鉴于Google Apps脚本是Javascript 1.6(即不是ES5 +),这可能是日期比较失败的原因。鉴于您知道从 task.due 获取的严格的特定格式,使用自定义解析器可能是更好的方法。 此答案中详细介绍了一个这样的解析器。

Note that this is equivalent to calling Date.parse() and thus could be the reason your date comparisons fail, given that Google Apps Script is Javascript 1.6 (i.e. not ES5+). A possibly better method would be to use a custom parser, given that you know the strict specific format that you will get from task.due. One such parser is detailed in this answer.

如< href = https://stackoverflow.com/users/1368381/serge-insas> @Serge 提及,您应该使用本机 Date 进行此比较对象,而不是字符串

As @Serge mentions, you should perform this comparison using native Date objects, and not Strings:

function isOverdue(task) {
  if(!task.due)
    return false;

  var now = new Date();
  var endOfToday = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1)); // Also known as start of tomorrow ;)
  var taskDueDate = myFunctionToConvertRFC3339StringToJSDate(task.due);
  return taskDueDate < endOfToday;
}

这篇关于Google格式的日期与格式比较在Google Apps脚本中落后1天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆