在 Google Apps Script 中,您可以使用 Utilities.formatDate() 方法将日期格式化为特定的字符串格式。例如,以下代码将日期格式化为 "MM/dd/yyyy" 格式。
var date = new Date();
var formattedDate = Utilities.formatDate(date, "GMT", "MM/dd/yyyy");
您可以使用 Date.addMonths() 方法将月份添加到日期中。以下是一个示例代码,该代码从现在开始的下一个月添加三个月,并输出格式化的日期。
var now = new Date();
var nextMonth = now.addMonths(1);
var threeMonthsLater = nextMonth.addMonths(3);
var formattedDate = Utilities.formatDate(threeMonthsLater, "GMT", "MM/dd/yyyy");
Logger.log(formattedDate); // 输出格式化的日期
请注意,addMonths() 方法是自定义的,需要手动将其添加到 Date 原型中,以便在 Google Apps Script 中使用。以下是实现该方法的代码。
Date.prototype.addMonths = function (n) {
var d = new Date(this.valueOf());
d.setMonth(d.getMonth() + n);
return d;
}