John Smith John Smith - 15 days ago 6
Javascript Question

Dateformat change from PST to GMT

Trying to convert variable preDate (PST) to GMT format without success, iv'e tried two different approaches.

preDate = "1/25/2016" (current value)

postDate = "25-1-2016" or "25/1/2016" (value i wish to have)

Function A)

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

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



Cannot find method formatDate(string,string,string)


Function 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

Answer

The formatDate method requires the first parameter as Date and not String. Here's a modified version that should work. Make sure the default timezone of your Google Script is set as PST.

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