There are lot of questions regarding the date-time conversions but need the right way to do it. My requirement is to take the user Input "DateTime" from a field and convert it to the specified timeZone dateTime as shown below. My code is like this

BusinessHours testHours = [Select Id, TimeZoneSidKey from BusinessHours where id = 'businessHoursId'];

Case c = [SELECT Id, SlaStartDate FROM Case WHERE Id='caseId'];

system.debug(DateTime.valueOf((c.SlaStartDate.format('yyyy-MM-dd HH:mm:ss', testHours.TimeZoneSidKey))));

Let's say, if I want to convert "SlaStartDate" to PST dateTime then the above debug log stores Datetime value in GMT. Any help over the conversion?

1 Answer 1


Your approach is perfect to convert values according to specific Timezone as:

System.debug(' Current Time is '+ DateTime.now() );
System.debug(' Converted Time to PST is ' +
                  DateTime.now().format('MM/dd/yyyy HH:mm:ss',

However you can convertTimezone SOQL function in your query. It converts DateTime field to the user’s time zone.:

Case c = [SELECT Id, convertTimezone(SlaStartDate) FROM Case WHERE Id='caseId'];
  • This will give an Invalid aggregate function: convertTimezone error.
    – Folkert
    Apr 12, 2018 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.