创建一个 returns 日期在 Google 表格中的函数
Creating a function that returns a date in Google Sheets
我创建了一个函数,它接受一个日期和月数以添加到该日期和 returns 下一个日期。该功能似乎运行良好,我使用 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());
日志中的日期与调试器中的日期不匹配。有人见过这个吗?另外,有谁知道如何获取数字的整数值?我试了一下,但得到了一些奇怪的结果。
例如:调试器将值显示为 2020 年 12 月 1 日星期二 00:00:00 GMT-0500(东部标准时间)但日志将值显示为 returnDate(1): 11.0 / 2.0 / 2020.0
问题:
getMonth
returns the month from 0-11, where 0 represents January and 11 represents December. getDay
returns 星期几 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"
我创建了一个函数,它接受一个日期和月数以添加到该日期和 returns 下一个日期。该功能似乎运行良好,我使用 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());
日志中的日期与调试器中的日期不匹配。有人见过这个吗?另外,有谁知道如何获取数字的整数值?我试了一下,但得到了一些奇怪的结果。
例如:调试器将值显示为 2020 年 12 月 1 日星期二 00:00:00 GMT-0500(东部标准时间)但日志将值显示为 returnDate(1): 11.0 / 2.0 / 2020.0
问题:
getMonth
returns the month from 0-11, where 0 represents January and 11 represents December. getDay
returns 星期几 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"