John Smith
John Smith

Reputation: 397

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

Upvotes: 0

Views: 103

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11278

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);
}

Upvotes: 1

Related Questions