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
Dhaval PanchalDhaval Panchal 

Date Time Formatting

Hi,

 

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.

souvik9086souvik9086

Check this

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

 

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.

Thanks

levaleva
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
levaleva

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.
levaleva

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 

levaleva

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.

https://workbench.developerforce.com/select.php

you can login using your salesforce account.