简体   繁体   English

如何使用Google Apps脚本获取上个月日期的正确格式

[英]How to get the right format for dates in the previous month with Google Apps Script

Using Google Apps Script, I am getting the first and last date from the previous month and then change the format to GMT 'yyyy-MM-dd' 使用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');
    }

But I get an error stating: 但我得到一个错误说明:

" Invalid value ' function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); } '. Values must match the following regular expression: '[0-9]{4}-[0-9]{2}-[0-9]{2}|today|yesterday|[0-9]+(daysAgo)' " 无效值'函数getLastDate(){var d = todayDate; d.setDate(0); return Utilities.formatDate(e,“ GMT”,“ yyyy-MM-dd”);}'。值必须与以下常规值匹配表达式:'[0-9] {4}-[0-9] {2}-[0-9] {2} |今天|昨天| [0-9] +(daysAgo)'

Can someone please help? 有人可以帮忙吗?

You seem to be expecting those variables to contain dates, but the way you declare them does not assign to them the return value of the associated functions but the functions themselves. 您似乎希望这些变量包含日期,但是声明它们的方式并未为它们分配关联函数的返回值,而是函数本身。

You expect lastDate to contain: 您希望lastDate包含:

2015-07-31

but it actually contains: 但实际上包含:

function getLastDate() { var d = todayDate; d.setDate(0); return Utilities.formatDate(e, "GMT", "yyyy-MM-dd"); } 

You need to separate the assignment from the declaration: 您需要将分配与声明分开:

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);

But it looks like we don't even really need to keep those functions. 但是看起来我们甚至根本不需要保留这些功能。 We can turn them into IIFE . 我们可以将它们变成IIFE We should probably also avoid reusing the same Date object: 我们可能还应该避免重用相同的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);

If you want to get previous month and locale it into spanish, check this out.. 如果要获取上个月并将其本地化为西班牙语,请查看此内容。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM