Tuesday, 20 October 2015

How to convert a datetime to specific timezone in salesforce

Here is the simple steps to convert a datetime value to the specific timezone in salesforce,

Step 1:
Build a specific date

Step 2:
Build a specific time.

Step 3:
Build DateTime instance and pass Date and Time values in parameter.

Step 4:
Convert the specified time to GMT using timeGMT() method.

Step 5:
Build a datetime with GMT and pass Date and Converted GMT time values in the parameter.

Step 6:
To convert a datetime to the specific timezone, provide the datetime format along with time zone.

  1. Date myDate = date.newinstance(20151020);
  2. Time myTime = Time.newInstance(233500);
  3. DateTime dt = DateTime.newInstance(myDate, myTime);
  4. Time myGmtTime = dt.timeGMT();
  5. System.debug('GMT Time'+dt.timeGMT());
  6. DateTime dtt = DateTime.newInstanceGmt(myDate, myGmtTime);
  7. String strConvertedDate = dtt.format('MM/dd/yyyy HH:mm:ss''Asia/Kolkata');
  8. System.debug('Converted time'+strConvertedDate);


Reference:


No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...