I have two fields: one Datetime I need to show in user timezone and another in GMT. How to show Datetime in UI in GMT format? Because in UI it always converted to user timezone. So my second field should be in GMT but it in user time zone
Best Answer
Use a formula text field to present the GMT value, like:
TEXT(TheDateTimeForShowingInGMT__c)
Then show that formula on the layout instead of TheDateTimeForShowingInGMT__c.
Looks like DateTime.parse() takes the string and parses it based on the context users locale to produce a GMT date/time in the database. DateTime.format(String) should create an output string in based on the timezone of the current context user.
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'];
Best Answer
Use a formula text field to present the GMT value, like:
Then show that formula on the layout instead of TheDateTimeForShowingInGMT__c.