编写一个Google Apps脚本,通过Google Tasks API(作为高级服务)查看任务,并检查其截止日期以查看它们是否已过期。如果是,则应将它们移至当天。
我使用此代码创建此项目以保持Google Tasks的最新状态:
将当前日期与任务到期日期进行比较时,任务到期日始终落后一天。
调查结果我发现它可能与夏令时有关,只需一小时就可能意味着两天内的差异。我发现this question有类似问题,他们的解决方案是使用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
将formatDate中的"EST"
更改为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
答案 0 :(得分:1)
根据Google Tasks API文档,task#due
property是RFC 3339日期时间值,例如"2018-04-11T0:45:26.000Z"
。
从这个字符串中,你可以原生地构造一个Javascript Date
,即
var taskDueDate = new Date(task.due);
请注意,这相当于调用Date.parse()
,因此可能是您的日期比较失败的原因,因为Google Apps脚本是Javascript 1.6(即不是ES5 +)。一个可能更好的方法是使用自定义解析器,因为您知道从task.due
获得的严格特定格式。一个这样的解析器在this answer中有详细说明。
如@Serge所述,您应该使用原生Date
个对象执行此比较,而不是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;
}
答案 1 :(得分:1)
对于info,这是我用来转换RFC339时间对象的小代码。 我发现阅读和理解更容易。
function parseDate_RFC3339(string) {
var refStr = new Date().toString();
Logger.log('refStr = '+refStr);
var tzOffset = Number(refStr.substr(refStr.indexOf('GMT')+4,2));
Logger.log('TZ offset = '+tzOffset);
var parts = string.split('T');
parts[0] = parts[0].replace(/-/g, '/');
var t = parts[1].split(':');
return new Date(new Date(parts[0]).setHours(+t[0]+tzOffset,+t[1],0));
}