创建一个返回Google表格中日期的函数 [英] Creating a function that returns a date in Google Sheets

查看:42
本文介绍了创建一个返回Google表格中日期的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a function that takes in a date and number of months to add to that date and returns the next date. The function seems to be working perfectly, which I check using DEBUG. The strange thing is when I logger in the returned date using the line below,

monthstoadd = 18

date1.setFullYear(2019, 6, 1); 

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

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

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

the date in the log does not match the date in the debugger. Has anyone seen this? Also, does anyone know how to get the integer value of a number? I tried round but got some strange results.

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

For instance: Debugger show value as Tue Dec 01 2020 00:00:00 GMT-0500 (Eastern Standard Time) but log shows value as returnDate(1): 11.0 / 2.0 / 2020.0

推荐答案

问题:



getMonth 返回0到11的月份,其中0代表一月,11代表十二月。 getDay 返回星期几0-6,其中0代表星期日,6代表星期六。因此,日志:

Issue:

getMonth returns the month from 0-11, where 0 represents January and 11 represents December. getDay returns Day of the week 0-6, where 0 represents Sunday and 6 represents Saturday. So, the log:

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"

这篇关于创建一个返回Google表格中日期的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆