我是Google App Script的新手,目前正在研究一个项目,以帮助自己熟悉它。我的项目有一部分需要将嵌套JSON中的Unix Timestamp对象转换为人类可读的时间。由于我不知道如何在Google App脚本中转换时间戳,因此我查看了文档并找到了“ Utilities.formatDate()”。
我尝试在示例时间戳上使用它,以查看其工作方式以及是否可以满足我的需要。因此,我从数据中获取了时间戳,并尝试使用此代码对其进行转换。
function blah () {
var tim = "1572401067";
var formattedDate = Utilities.formatDate(tim, "GMT+5:30", "MM-dd-yyyy HH:mm:ss");
Logger.log(formattedDate);
}
以错误结尾:
Cannot find method formatDate(string,string,string). (line 3, file "Code")Dismiss
我在这里做错了什么?
正如您的错误消息正确描述的那样,没有诸如formatDate(string,string,string)
的功能。 GAS中存在的formatDate
函数采用三个参数,其中第一个是Date
,第二个和第三个是string
。您的代码更正如下所示:
function blah() {
var tim = 1572401067;
var date = new Date(tim*1000);
var formattedDate = Utilities.formatDate(date, "GMT+5:30", "MM-dd-yyyy HH:mm:ss");
Logger.log(formattedDate);
}