【问题标题】:Converting Unix timestamp in Google App scripts在 Google App 脚本中转换 Unix 时间戳
【发布时间】:2019-10-30 13:08:28
【问题描述】:

我是 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

我在这里做错了什么?

【问题讨论】:

标签: google-apps-script unix-timestamp


【解决方案1】:

正如您的错误消息正确描述的那样,没有 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);
}

【讨论】:

  • 谢谢。在最近发生的兴奋中,我完全忘记了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 1970-01-01
  • 2020-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多