+1 vote
by (163k points)

From UI I am getting this input:

Date : 2020-09-25 Time : 12:15 AM

Now when I try to save this to DB it saves as 2020-09-24T17:15:00.000Z

And when it gets shown in UI again after refresh the time changes to 12:15 PM

The logic is summarized below, try to run this, the final result changes to PM:

String strFormattedCorpTime = '12:15 AM'.split(' ')[0] + ':00' + '12:15 AM'.split(' ')[1];

System.debug(Datetime.valueOf('2020-09-25'+' '+strFormattedCorpTime).format('yyyy/dd/MM hh:mm a'));

I would like to save 12:15 AM in GMT format, so that while showing on UI it shows whatever user has saved datetime with. How can I do that?

1 Answer

+1 vote
by (163k points)
Best answer
String datetimeParseFormatGMT = Datetime.parse('09/25/2020' + ' ' + '12:15 AM').formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
Datetime dtNew = (Datetime) JSON.deserialize('"'+datetimeParseFormatGMT+'"', Datetime.class);


JSON.serialize was the key to convert datetime as string to datetime object.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.