【问题标题】:Formatting Dates in Google Apps Script and Google Spreadsheets在 Google Apps 脚本和 Google 电子表格中格式化日期
【发布时间】:2016-02-01 10:00:52
【问题描述】:
我有一个Google Spreadsheet 来管理我家人的食物储存。我使用 Google Apps 脚本的主要目的是在某些内容在 60 天内到期时向我发送电子邮件提醒。
您可以通过转到工具 > 脚本编辑器来查看脚本(您可能已经假设了。)
我在第 74 行和第 80 行遇到问题。当我运行脚本并检查日志时,我得到了 2015 年 12 月 32 日和 2015 年 11 月 31 日。如何让这些显示为 2016 年 1 月 1 日和 2015 年 12 月 1 日?
谢谢
【问题讨论】:
标签:
google-apps-script
google-sheets
【解决方案1】:
您可以使用Utilities.formatDate()。 Read more关于实用程序类。
替换第 74 行,
message1 += '1 ' + alertsArray[i][1] + ' of ' + alertsArray[i][0] + ' is going to expire on ' + (new Date(alertsArray[i][4]).getMonth() + 1) + '/' + (new Date(alertsArray[i][4]).getDate() + 1) + '/' + new Date(alertsArray[i][4]).getYear() + '. <br/>';
有了这个,
message1 += '1 ' + alertsArray[i][1] + ' of ' + alertsArray[i][0] + ' is going to expire on ' + Utilities.formatDate(new Date(alertsArray[i][4]), "GMT-07:00", "''MM/dd/yyyy")) + '. <br/>';
对第 80 行执行相同操作。