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
-
1what are you using for UI?– salesforce-sasCommented Aug 4, 2020 at 12:09
-
I.e. are you showing these in a standard record form, through a Visualforce page, using an Aura component or an LWC? Please edit your question to add more detail and set some tags on the question too. Ta.– Phil WCommented Aug 4, 2020 at 12:10
-
I'm using Datetime format, so convert to String is not suitable for me– Igor VasylevskyiCommented Aug 4, 2020 at 12:10
-
1@PhilW it standard page layout, problem is to show datetime in gmt– Igor VasylevskyiCommented Aug 4, 2020 at 12:15
Add a comment
|
2 Answers
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.
-
I know about it but I need to keep it in Datetime format Commented Aug 4, 2020 at 12:31
-
1
-
Thanks @PhilW for response, I already found right solution Commented Aug 4, 2020 at 12:56
Datetime userTime = [datetime in user timezone];
Datetime datetimeInGmt = Datetime.valueOf(userTime.format('yyyy-MM-dd', 'GMT'));
Now it is displayed properly in UI (first is Datetime in user timezone and second Work Start field in GMT)
-
1Where are you storing this value for presentation in a standard record? This answer doesn't provide the whole story. You show a way to convert a date/time value to GMT in Apex which is quite different from presentation.– Phil WCommented Aug 4, 2020 at 13:15
-
@PhilW I'm storing this value for presentation in a standard record in record page layout. Problem was to show time in GMT because even Datetime.now() shows time in GMT in DevConsole but in UI it always in user timezone Commented Aug 4, 2020 at 13:49
-
1Please edit the answer to show how you store the value to present in the layout and I will upvote your answer. (There are lots of other ways to do this, including applying timezone offsets, that don't require a datetime to string to datetime conversion)– Phil WCommented Aug 4, 2020 at 13:57
-
Adding the picture does not show how you are storing the value for presentation. I assume you write the "datetimeInGmt" to another Datetime field in your object?– Phil WCommented Aug 4, 2020 at 14:49
-