0

enter image description hereI 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

4
  • 1
    what are you using for UI? Commented 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 W
    Commented Aug 4, 2020 at 12:10
  • I'm using Datetime format, so convert to String is not suitable for me Commented Aug 4, 2020 at 12:10
  • 1
    @PhilW it standard page layout, problem is to show datetime in gmt Commented Aug 4, 2020 at 12:15

2 Answers 2

1

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.

3
  • I know about it but I need to keep it in Datetime format Commented Aug 4, 2020 at 12:31
  • 1
    This is just for presentation, right?
    – Phil W
    Commented Aug 4, 2020 at 12:50
  • Thanks @PhilW for response, I already found right solution Commented Aug 4, 2020 at 12:56
1

enter image description hereRight answer is:

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)

5
  • 1
    Where 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 W
    Commented 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
  • 1
    Please 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 W
    Commented 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 W
    Commented Aug 4, 2020 at 14:49
  • @PhilW yes it's another field with the same label Commented Aug 5, 2020 at 6:31

You must log in to answer this question.

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