如何使用 Google Apps 脚本获取上个月日期的正确格式
How to get the right format for dates in the previous month with Google Apps Script
使用 Google Apps 脚本,我获取上个月的第一个和最后一个日期,然后将格式更改为 GMT 'yyyy-MM-dd'
var todayDate = new Date();
var lastDate = function getLastDate() {
var d = todayDate;
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
}
var firstDate = function getFirstDate() {
var e = todayDate;
e.setMonth(e.getMonth() - 1);
e.setDate(1);
return Utilities.formatDate(e, 'GMT', 'yyyy-MM-dd');
}
但我收到一条错误消息:
"无效值' function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); } '。值必须匹配以下正则表达式:'[0-9]{4}-[0-9]{2}-[0-9]{2}|today|昨天|[0-9]+(daysAgo)'"
有人可以帮忙吗?
您似乎期望这些变量包含日期,但您声明它们的方式并没有为它们分配关联函数的 return 值,而是函数本身。
您希望 lastDate
包含:
2015-07-31
但实际上包含:
function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); }
您需要将赋值与声明分开:
var todayDate = new Date();
function getLastDate() {
var d = todayDate;
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
}
var lastDate = getLastDate();
function getFirstDate() {
var e = todayDate;
e.setMonth(e.getMonth() - 1);
e.setDate(1);
return Utilities.formatDate(e, 'GMT', 'yyyy-MM-dd');
}
var firstDate = getFirstDate();
// check values
Logger.log(lastDate);
Logger.log(firstDate);
但看起来我们甚至不需要保留这些功能。我们可以把它们变成IIFE。我们可能还应该避免重复使用相同的 Date
对象:
var lastDate = (function() {
var d = new Date();
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
})();
var firstDate = (function() {
var d = new Date();
d.setMonth(d.getMonth() - 1);
d.setDate(1);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
})();
// check values
Logger.log(lastDate);
Logger.log(firstDate);
如果您想获取上个月的信息并将其语言环境设置为西班牙语,请查看此内容..
var previousMonth = (function() {
var d = new Date();
d.setMonth(d.getMonth() - 1);
var getLastMonth = Utilities.formatDate(d,Session.getScriptTimeZone(),"MMMM");
var translateMonthIntoSpanish = LanguageApp.translate (getLastMonth,'en','es');
var titleCaseMonth = translateMonthIntoSpanish.charAt(0).toUpperCase() + translateMonthIntoSpanish.slice(1);
return titleCaseMonth;
})();
Logger.log(previousMonth);
使用 Google Apps 脚本,我获取上个月的第一个和最后一个日期,然后将格式更改为 GMT 'yyyy-MM-dd'
var todayDate = new Date();
var lastDate = function getLastDate() {
var d = todayDate;
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
}
var firstDate = function getFirstDate() {
var e = todayDate;
e.setMonth(e.getMonth() - 1);
e.setDate(1);
return Utilities.formatDate(e, 'GMT', 'yyyy-MM-dd');
}
但我收到一条错误消息:
"无效值' function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); } '。值必须匹配以下正则表达式:'[0-9]{4}-[0-9]{2}-[0-9]{2}|today|昨天|[0-9]+(daysAgo)'"
有人可以帮忙吗?
您似乎期望这些变量包含日期,但您声明它们的方式并没有为它们分配关联函数的 return 值,而是函数本身。
您希望 lastDate
包含:
2015-07-31
但实际上包含:
function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); }
您需要将赋值与声明分开:
var todayDate = new Date();
function getLastDate() {
var d = todayDate;
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
}
var lastDate = getLastDate();
function getFirstDate() {
var e = todayDate;
e.setMonth(e.getMonth() - 1);
e.setDate(1);
return Utilities.formatDate(e, 'GMT', 'yyyy-MM-dd');
}
var firstDate = getFirstDate();
// check values
Logger.log(lastDate);
Logger.log(firstDate);
但看起来我们甚至不需要保留这些功能。我们可以把它们变成IIFE。我们可能还应该避免重复使用相同的 Date
对象:
var lastDate = (function() {
var d = new Date();
d.setDate(0);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
})();
var firstDate = (function() {
var d = new Date();
d.setMonth(d.getMonth() - 1);
d.setDate(1);
return Utilities.formatDate(d, 'GMT', 'yyyy-MM-dd');
})();
// check values
Logger.log(lastDate);
Logger.log(firstDate);
如果您想获取上个月的信息并将其语言环境设置为西班牙语,请查看此内容..
var previousMonth = (function() {
var d = new Date();
d.setMonth(d.getMonth() - 1);
var getLastMonth = Utilities.formatDate(d,Session.getScriptTimeZone(),"MMMM");
var translateMonthIntoSpanish = LanguageApp.translate (getLastMonth,'en','es');
var titleCaseMonth = translateMonthIntoSpanish.charAt(0).toUpperCase() + translateMonthIntoSpanish.slice(1);
return titleCaseMonth;
})();
Logger.log(previousMonth);