function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajesh SFDCRajesh SFDC 

converting datetime problesm to 12 hours format in apex

The following value is my input
'11/21/2014 15:20:00'
i want to change the above value format like '11/21/2014 03:20 pm'
how to do this any help me

i am getting errors like this "System.TypeException: Invalid date/time: 11/21/2014 15:20:00"
Sforce.NinjaSforce.Ninja
Hi Rajesh,
What you need to do is format the date. Why don't you do it this way use Format method to format the date.
Try parsing it using
 
Datetime myDateTime = DateTime.parse('11/21/2014 15:20:00');
And then format the myDateTime using
String dtConverted = myDateTime.format('MM/dd/yyyy HH:mm a'));
Note: Not tested it.

If it helps, please mark this as solved.


 
BalajiRanganathanBalajiRanganathan
Try this code
String input = '11/21/2014 15:20:00';
input = input.subString(6,10) + '-' + input.subString(0,2) + '-' + input.subString(3,5) + 'T'+input.subString(11);
DateTime dt = (DateTime) JSON.deserialize('"' + input + '"', DateTime.class);
System.debug(dt.format());

 
Pragadheeswaran KandasamyPragadheeswaran Kandasamy
Hi bro,
Try the following code, it will be usefull for you

<apex:outputText value="{0,date,M/d/yyyy hh:mm a }">
      <apex:param value="{!Chat.h_message_sent_on__c}" />
</apex:outputText>
indyindy
Balaji - I followed your approach but the datatime value is showing 4 hours less than the actual date time. i.e it is converting the date time value to my local time zone in Salesforce UI. Could please help me here on how to get desired format without time zone.

Thanks,
Indy