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?
Best Answer
Your approach is perfect to convert values according to specific Timezone as:
However you can
convertTimezone
SOQL function in your query. It converts DateTime field to the user’s time zone.: