Boosting Your CRM Productivity

API Support

Salesforce developers can also access the local time of a specified country by making calls to the Local Time API from either Apex or Javascript code. Following are the available API methods.

1. tz.LocalTime.getOffset

Arguments: String country, String state, String city

Return Type: JSON String
Example: {“offset”:”-8″,”abbreviation”:”PST”,”region”:”NA”,”tz_iana”:”America/Los_Angeles”}

Description: Returns the UTC/GMT offset of a given country and the timezone abbreviation as a JSON string. In case of error, the return values are set to ‘N/A’.
Depending on the country (mandatory value), the values for state and city arguments could be optional. The values given should be consistent with those shown in Appendix A – Supported Country List.

Apex Examples:

// Alabama, USA. Returns -6.00 or -5.00 (if DST is set)
String json = tz.LocalTime.getOffset('United States', 'AL', '');

// France. Returns 1.00 or 2.00 (if DST is set)
String json = tz.LocalTime.getOffset('France', '', '');

// Russia. Returns 4.00
String json = tz.LocalTime.getOffset('Russian Federation', '', 'Moscow');

// Getting the local time based on the above json strings
JSONParser parser = JSON.createParser(json);

parser.nextToken();
parser.nextValue();
String offset = parser.getText(); // timezone UTC/GMT offset

parser.nextToken();
parser.nextValue();
String abbr = parser.getText(); // abbreviation (PST, EST etc..)

datetime dt = tz.LocalTime.getLocalTimeByOffset(Decimal.valueof(offset));
String sTime = dt.format('dd/MM/yyyy hh:mm:ss a');

system.debug('Local Time: ' + sTime);

Javascript Example:

var args = {country:"United States",state:"AL",city:""}; 
var jsontext = sforce.apex.execute("tz.LocalTime","getOffset",args); 
var lt=JSON.parse(jsontext);
var sOffset=lt.offset;
var sAbbr=lt.abbreviation; 

2. tz.LocalTime.getOffsetByAreaCode

Arguments: String phone

Return Type: JSON String
Example: {“offset”:”-8″,”abbreviation”:”PST”,”region”:”NA”,”tz_iana”:”America/Los_Angeles”}

Description: Returns the UTC/GMT offset of a given country and the timezone abbreviation as a JSON string. In case of error, the return values are set to ‘N/A’.
The phone number should be given as a string and its value should be consistent with those shown in Appendix B – Supported Phone Formats.

Apex Examples:

// California, USA. Returns -8.00 or -7.00 (if DST is set)
String json = tz.LocalTime.getOffsetByAreaCode(‘415-234-1222’);

Javascript Example:

var args = {phone:"415-234-1222"}; 
var jsontext = sforce.apex.execute("tz.LocalTime","getOffsetByAreaCode",args); 
var lt=JSON.parse(jsontext);
var sOffset=lt.offset;
var sAbbr=lt.abbreviation; 

3. tz.LocalTime.getLocalTimeByOffset

Arguments: Decimal offset

Return Type: Datetime

Description: For a given UTC/GMT offset of a country, the above method returns the local time as a Salesforce Datetime object.

Apex Example:

// Get the local time for Alabama, USA.
datetime dt = tz.LocalTime.getLocalTimeByOffset(-6);
String sTime = dt.format('dd/MM/yyyy hh:mm:ss a');

Javascript Example:

var args = {offset:-6}; 
ltime = sforce.apex.execute("tz.LocalTime","getLocalTimeByOffset",args); 
document.write(ltime);

4. tz.LocalTime.getLocalTimeString

Arguments: Decimal offset

Return Type: String

Description: For a given UTC/GMT offset of a country, the method returns its local time as a string.

Apex Example:

// Get the local time for Alabama, USA.
String dt = tz.LocalTime.getLocalTimeString(-6);

Javascript Example:

var args = {offset:-6}; 
ltime = sforce.apex.execute("tz.LocalTime","getLocalTimeString",args); 
document.write(ltime);

5. tz.LocalTime.getOffsetFull

Arguments: String country, String state, String city

Return Type: JSON String
Example: {“offset”:”-5″,”dst_offset”:”-4″,”abbreviation”:”EST”,”dst_abbreviation”:”EDT”,”tz_name”:”Eastern Standard Time”,”dst_tz_name”:”Eastern Daylight Time”,”region”:”NA”,”tz_iana”:”America/New_York”}

Description: Returns full timezone information for given country as a JSON string. In case of error, the return values are set to ‘N/A’.

Apex Example:

// Get the full timezone information for New York, USA.
String json = tz.LocalTime.getOffsetFull('United States', 'NY', 'New York');

6. tz.LocalTime.getOffsetByAreaCodeFull

Arguments: String phone

Return Type: JSON String
Example: {“offset”:”-5″,”dst_offset”:”-4″,”abbreviation”:”EST”,”dst_abbreviation”:”EDT”,”tz_name”:”Eastern Standard Time”,”dst_tz_name”:”Eastern Daylight Time”,”region”:”NA”,”tz_iana”:”America/New_York”}

Description: Returns full timezone information based on a given phone number as a JSON string. In case of error, the return values are set to ‘N/A’.

Apex Example:

// Get the full timezone information for New York, USA.
String json = tz.LocalTime.getOffsetByAreaCodeFull('212-123-1234');

 

This site uses cookies. Find out more about this site’s cookies.