【问题标题】:Creating a function that returns a date in Google Sheets创建一个在 Google 表格中返回日期的函数
【发布时间】:2020-03-30 16:24:08
【问题描述】:

我创建了一个函数,该函数接受一个日期和月数以添加到该日期并返回下一个日期。该功能似乎运行良好,我使用 DEBUG 进行了检查。奇怪的是,当我使用下面的行记录返回的日期时,

monthstoadd = 18

date1.setFullYear(2019, 6, 1); 

returnDate = AddMonths(date1, monthstoadd);   // my selfmade function

Logger.log("returnDate(1):", returnDate.getMonth(), "/" , returnDate.getDay(), "/", returnDate.getFullYear());

日志中的日期与调试器中的日期不匹配。有人见过这个吗?另外,有谁知道如何获取数字的整数值?我试了一圈,但得到了一些奇怪的结果。

例如:调试器将值显示为 Tue Dec 01 2020 00:00:00 GMT-0500(东部标准时间),但日志将值显示为 returnDate(1): 11.0 / 2.0 / 2020.0

【问题讨论】:

  • 检查 tz @ File> projectproperties >timezone

标签: date google-apps-script google-sheets google-sheets-macros


【解决方案1】:

问题:

getMonth 返回从 0 到 11 的月份,其中 0 代表 1 月,11 代表 12 月。 getDay 返回星期 0-6,其中 0 代表星期日,6 代表星期六。所以,日志:

returnDate(1): 11.0 / 2.0 / 2020.0

正确且代表

returnDate(1): Dec / Tue / 2020.0

Tue Dec 01 2020 00:00:00 GMT-0500 (Eastern Standard Time) 

解决方案:

使用Intl:

console.info(new Intl.DateTimeFormat().format(returnDate))

console.log("returnDate(1):", returnDate.getMonth()+1, "/" , returnDate.getDate(), "/", returnDate.getFullYear());//Note Date vs Day and "+1"

【讨论】:

    猜你喜欢
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2011-01-01
    • 2011-02-19
    • 2015-11-23
    • 1970-01-01
    • 2020-08-15
    相关资源
    最近更新 更多