I am getting the time as string like "client_modified": "2020-02-04T11:27:34Z" How can I convert this to local time zone,


I am getting this time from api response I need to display it in lightning:formatteddatetime, Sort those records based on date time so am doing this conversion in controller side


string s = string.valueof(a2.get('server_modified'));
DateTime LDate = null;
if(s != null && s != ''){
    s = s.replace('T', ' ');
    s = s.replace('Z', '');
    LDate = DateTime.valueof(s);
  • 3
    In most situations, you don't need to do anything. Salesforce stores datetimes in UTC, and converts to whatever the user's locale is when displayed on something like a page layout or visualforce page (if you're using <apex:outputField>). You should edit your question to include more details.
    – Derek F
    Feb 4, 2020 at 12:18
  • @DerekF Updated Feb 4, 2020 at 12:50
  • so does the order change if you change the timezone and then sort with datetime?
    – User6670
    Feb 4, 2020 at 13:09

1 Answer 1


As @Derek mentioned, Salesforce store the time in UTC but when we use apex:outputfield in VF page, Salesforce convert the datetime to based on user locale so you don't need to do anything.

If you still have any other use case then I suggest use below code to convert

Use Date.format() method and pass the time Zone. It will convert the time for you

//Get current date time in GMT format
Datetime dt = System.now();

//Convert it into Indian Standard Time(dtIST).
String dtIST = dt.format('yyyy-MM-dd HH:mm:ss', 'IST');

System.debug('dt-' + dt);
System.debug('dtIST-' + dtIST);
  • I am getting date time as string from api response, so I wanted to convert it into date time Feb 4, 2020 at 14:23
  • please check the code i updated Feb 4, 2020 at 14:25
  • please check updated code @Ratan Paul Feb 5, 2020 at 7:46
  • @nubie Can you give the value of a2.get('server_modified') ?
    – Ratan Paul
    Feb 5, 2020 at 15:10
  • 2020-02-04T11:27:34Z @Ratan Paul Feb 6, 2020 at 5:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .