I have a DateTime field which shows proper time on UI, I know behind the scene it is GMT format. However I would like to populate same format as it displays on UI in a text field along with some text.
-
1Since different users accessing your UI may have different time zones, pre-rendering the value into a string is not a good idea. Better to have the system dynamically render the date time on demand.– Phil WMar 28, 2019 at 23:40
-
Yeah, that make sense. I will do that.– AjayMar 29, 2019 at 8:54
1 Answer
Yes, formula is not valid for this requirement, so I used trigger to update the text field. date.format() is the savior.
String timeZone = UserInfo.getTimeZone().getID();
Datetime dateGMT = info.Info_Session_Date__c;
Datetime dateTimeValue = Datetime.valueOf(dateGMT);
string convertedString = dateTimeValue.format();