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
K RoyK Roy 

I have set a date field, and after fetching a value from it in the controller, i want to show the date field in the form of AM/PM. Can anyone help me in this?

Best Answer chosen by K Roy
Khan AnasKhan Anas (Salesforce Developers) 
Hi Rahul,

To convert Date/Time in 12 hrs format you can use:
"Date/time field".format('hh:mm:ss a');

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Rahul,

To convert Date/Time in 12 hrs format you can use:
"Date/time field".format('hh:mm:ss a');

I hope it helps you.

Kindly let me inform if it helps you and close your query by marking it as solved so that it can help others in future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Asitha_RajuAsitha_Raju
Hi Rahul,
You can use the format('hh:mm a'). Here 'a' denotes whether its AM/PM.
DateTime myDateTime =DateTime.now();
String Timec = myDateTime.format('hh:mm a');
Refer the link : https://paulforce.wordpress.com/2009/08/27/formatting-time-in-apex/ for more information.
Please try this out and let me know if it works. Dont forget to mark it as best answer if it helps you.
Thanks.
K RoyK Roy
Thanks for the resolution