编写一个 Google Apps 脚本,通过 Google Tasks API(作为一项高级服务)查看任务并检查它们的截止日期以查看它们是否过期。如果是,则应将它们移至当前日期。
已完成的项目
我使用此代码创建此项目以使 Google Tasks 保持最新:
问题
当比较当前日期和任务到期日期时,任务到期日期总是晚一天。
研究
调查了一下,我发现它可能与夏令时有关,而仅仅休息一小时可能意味着两天内的差异。我发现这个问题有类似的问题,他们的解决方案是用来Session.getScriptTimeZone()
正确设置时区。
但是,即使我使用Session.getScriptTimeZone()
它设置时区,任务的截止日期仍然晚了一天。
编码
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.');
}
}
日志
[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
,它似乎有正确的日期。所以关于格式的一些事情正在改变日期。
新代码
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.');
}
}
新日志
[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()
删除.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.');
}
}
新日志
[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