需要考虑的几件事。
1. 在脚本项目属性中设置时区。文件,项目属性。
2. 使用Utilities.formatDate() 可以减轻很多头痛,因为您可以根据需要标准化日期。
3. getDay() 和 getDate() 分别返回“星期几(即 1-7)”和“月份中的日期(即 1-31)”。
这里有一些关于日期的更多细节需要澄清:
function addDates() {
var prevDateCurrYear = new Date();
var year = Utilities.formatDate(prevDateCurrYear, "America/Denver", "yyyy");
var date = Utilities.formatDate(prevDateCurrYear, "America/Denver", "d");
var month = Utilities.formatDate(prevDateCurrYear, "America/Denver", "M");
Logger.log("\nUnformatted prevDateCurrYear: "+prevDateCurrYear+
"\n\nYour original log:\nprevDateCurrYear.getMonth(): "+prevDateCurrYear.getMonth()+"\n"+
"prevDateCurrYear.getDay(): "+prevDateCurrYear.getDay()+"\n"+
"prevDateCurrYear.getYear(): "+prevDateCurrYear.getYear()+"\n"+
"\nUsing Utilites.formatDate():\nmonth: "+month+"\ndate: "+date+"\nyear: "+year+
"\nUtilities.formatDate(prevDateCurrYear, \"America/Denver\", \"M d yyyy\"): "
+Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"));
}
日志:
未格式化的 prevDateCurrYear:2016 年 2 月 18 日星期四 11:29:42 GMT-0700 (MST)
您的原始日志:
prevDateCurrYear.getMonth(): 1
prevDateCurrYear.getDay(): 4
prevDateCurrYear.getYear(): 2016
使用 'Utilites.formatDate()`:
月:2
日期:18
年份:2016
Utilities.formatDate(prevDateCurrYear, "America/Denver", "M d yyyy"): 2 18 2016