1

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

2

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',
                  'America/Los_Angeles')
             );

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'];
1
  • This will give an Invalid aggregate function: convertTimezone error.
    – Folkert
    Commented Apr 12, 2018 at 8:42

You must log in to answer this question.

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