Vinay Joseph
Vinay Joseph

Reputation: 5655

Google App Script - Date as String Manipulation

I am looking for a Google App Script function, which will take the following text

Sat May 12 2012 00:00:00 GMT+1000 (EST)

and return

Sat May 12 2012

Is it just like javascript ?

Upvotes: 0

Views: 265

Answers (2)

Mogsdad
Mogsdad

Reputation: 45720

Best if you use the built-in utilities, which can cope with the time zone properly, rather than using string manipulation. If the date is available as a Date object (e.g. read from Google Spreadsheet), you can just pass it to Utilities.formattedDate().

function myFunction() {
  var myDateString = "Sat May 12 2012 00:00:00 GMT+1000 (EST)"; // Want Sat May 12 2012
  var formattedDate = Utilities.formatDate(new Date(myDateString),
                                           "GMT+1000", "EEE MMM dd yyyy");
  Logger.log(formattedDate);
}

Logging Output:

[14-07-11 22:32:02:438 EST] Sat May 12 2012

Note that the given time is GMT+10, while my script is running in GMT-5 "EST".

Upvotes: 1

Alan Wells
Alan Wells

Reputation: 31300

if

Sat May 12 2012 00:00:00 GMT+1000 (EST)

is a string, you can just use a string function:

var myDateString = "Sat May 12 2012 00:00:00 GMT+1000 (EST)";
var shorterDateStrng = myDateString.slice(0,myDateString.indexOf(":")-2);
//If this is in .gs code
Logger.log('shorterDateStrng: ' + shorterDateStrng );

Apps Script uses JavaScript.

Upvotes: 0

Related Questions