+ Start a Discussion
Dhaval PanchalDhaval Panchal 

Date Time Formatting



I want to convert datetime value


i.e. currently I am getting value "Mon Aug 26 11:49:39 GMT 2013" and I want to convert it to "26-Aug-13 11:49 PM"


Any Ideas?


I want to apply formatting on standard "CreatedDateTime" field.


Check this




You will get the format method there.


If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.


DateTime  dt = DateTime.now();
dt.format('dd-MMM-YY hh:mm a');


Dhaval PanchalDhaval Panchal
Hi Leva,

Now i am getting below error.

Unrecognized format: dd-MMM-YY hh:mm a

could you post your code.

What happens if you just run those 2 lines in execute anonymous?

Dhaval PanchalDhaval Panchal
I want to change date format of CreatedDate field.

String strDateTime = con.createdDate.format('dd-MMM-YY hh:mm a');

where con is object of Contact. I does not giving me compile error but it throws runtime error.

Which editor are you using? The screenshot does not look like Eclipse or Developer Console (I tried it in both, worked ok). Also, I noticed you have 'CreateDate' field. Is it Date or DateTime, the code will work on DateTime types not on Date objects 


A colleague managed to replicate your issue. It does not work in old sandboxes. If you have a recently refreshed box try it there 

Dhaval PanchalDhaval Panchal
Its a Workbench, this tool is provided by salesforce.


you can login using your salesforce account.