Wednesday, July 17, 2013

Google Spreadsheet Script to get Time Zone Names

Question:

( by joegez )


I am trying to create a datetimestamp which includes the timezone.  

I implemented the following lines of code to get the timezone and print it out.

var TimeZone=Session.getTimeZone();
Logger.log(TimeZone);

The resulting line the in the log is "[13-07-16 19:13:46:767 PDT] America/Los_Angeles".

Even though the script is aware that I am working in PDT it returns a timezone of  "[13-07-16 19:13:46:767 PDT] America/Los_Angeles".  I would prefer to get "PDT" as a result.

Joe Gezelter


Solution:


To get the Time Zone Name in Google Spreadsheet Script or you can say in Google Apps Scripts, you can use the following code which I have made using my Spreadsheet which contains the list of all Time Zones.


Have a look at the following code:

///////////////////////////////////////




function logTimeZone() {
  var timeZone = Session.getTimeZone();
  var TimeZone = getTimeZoneXXX(timeZone);
  Logger.log(TimeZone);
}

function getTimeZoneXXX(timeZone) {
  var values = SpreadsheetApp.openById("0AmMTqpzD9YRndGQ4VVlFemtSd1RpcThkS1hfMFdZSVE").getSheetByName("List").getDataRange().getValues();
  for(var i=0;i<values.length;i++)
    if(values[i][0]==timeZone) return values[i][2];
  return "#NA";
}


///////////////////////////////////////

put the above code in your script editor, and then you can use it directly in your Google Spreadsheet or you can use its functionality in the Google Apps Script.



Have a look at the following screenshot of my Spreadsheet:




And here is the URL of my Spreadsheet:

https://docs.google.com/spreadsheet/ccc?key=0AmMTqpzD9YRndGQ4VVlFemtSd1RpcThkS1hfMFdZSVE#gid=0


And If you are not much familiar with scripts then check out the following link:
http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html 

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

No comments:

Post a Comment