[Custom function] Get the current date and time by the timezone

Here is a custom function to get the current date and time by the timezone.

 

Source code

// Get time by timezone
// now ... new Date()
// timezone ... GMT, UTC, ECT, JST
function getGlobalNowTime(tz){
  var dt = new Date();
  var ts = getTimezoneValue(tz);
  var fm = "dd/MM/yyyy HH:mm:ss";
  var res = Utilities.formatDate(dt, ts, fm);
  return res;
}

function getTimezoneValue(tz){
  var timeSt = "GMT+0:00";
  switch (tz){
    case "GMT": timeSt="GMT+0:00"; break;
    case "UTC": timeSt="GMT+0:00"; break;
    case "ECT": timeSt="GMT+1:00"; break;
    case "EET": timeSt="GMT+2:00"; break;
    case "ART": timeSt="GMT+2:00"; break;
    case "EAT": timeSt="GMT+3:00"; break;
    case "MET": timeSt="GMT+3:30"; break;
    case "NET": timeSt="GMT+4:00"; break;
    case "PLT": timeSt="GMT+5:00"; break;
    case "IST": timeSt="GMT+5:30"; break;
    case "BST": timeSt="GMT+6:00"; break;
    case "VST": timeSt="GMT+7:00"; break;
    case "CTT": timeSt="GMT+8:00"; break;
    case "JST": timeSt="GMT+9:00"; break;
    case "ACT": timeSt="GMT+9:30"; break;
    case "AET": timeSt="GMT+10:00"; break;
    case "SST": timeSt="GMT+11:00"; break;
    case "NST": timeSt="GMT+12:00"; break;
    case "MIT": timeSt="GMT-11:00"; break;
    case "HST": timeSt="GMT-10:00"; break;
    case "AST": timeSt="GMT-9:00"; break;
    case "PST": timeSt="GMT-8:00"; break;
    case "PNT": timeSt="GMT-7:00"; break;
    case "MST": timeSt="GMT-7:00"; break;
    case "CST": timeSt="GMT-6:00"; break;
    case "EST": timeSt="GMT-5:00"; break;
    case "IET": timeSt="GMT-5:00"; break;
    case "PRT": timeSt="GMT-4:00"; break;
    case "CNT": timeSt="GMT-3:30"; break;
    case "AGT": timeSt="GMT-3:00"; break;
    case "BET": timeSt="GMT-3:00"; break;
    case "CAT": timeSt="GMT-1:00"; break;
    default: break;
  }
  return timeSt;
}

 

Parameters

tz … timezone name (ex: GMT, UTC, ECT, JST…)

 

Responses

date and time (string format)

 

How to know the codes of time zones?

Please refer the following post;

The list of time zones

Leave a Comment