日期格式从PST更改为GMT

时间:2016-11-24 13:21:51

标签: javascript date google-apps-script

尝试将变量preDate(PST)转换为GMT格式但未成功,我尝试了两种不同的方法。

preDate =“1/25/2016”(当前值)

postDate =“25-1-2016”“2016/1/25”(我希望拥有的价值)

功能A)

function myFunction1() {
      var preDate = "1/25/2016";

      var formattedDate = Utilities.formatDate(preDate, "GMT", "d-MM-yyyy");
      Logger.log(formattedDate);
      }
  

找不到方法formatDate(string,string,string)

功能B)

function myFunction2(){
  var preDate = "1/25/2016";
  if (typeof preDate === "date"){
 var nydate = Utilities.formatDate(preDate, "GMT", "d-MM-yyyy"); 
  }
  Logger.log(nydate);
}
  

[16-11-24 14:15:16:532 CET] undefined

1 个答案:

答案 0 :(得分:1)

formatDate方法要求第一个参数为Date而不是String。这是一个应该有效的修改版本。确保您的Google脚本的默认时区设置为PST。

function myFunction1() {
  var preDate = new Date("1/25/2016");
  var formattedDate = Utilities.formatDate(preDate, "GMT", "d-MM-yyyy");
  Logger.log(formattedDate);
}