Skip to Content, PHP, MySQL, Javascript, Ajax, Htacces

How to change date formats using Apex Class?

Be First!

How to change date formats using Apex Class?

For Example 1:
2017-01-24 to change the date format to be in DD.MM.YYYY

Below example should work if you can use a string as the final output, otherwise dates are always displayed in the local context of the user.

Date d =;
String dt = DateTime.newInstance(d.year(),d.month(),'d-MM-YYYY');

For Example 2:
2017-01-24 00:00:00 to  01/24/2017 (mm/dd/yyyy)

This is already have a date ‘as a date’, but not as a string. The format about (2017-01-24 00:00:00) is just a visual representation of a value contained in variable of a DateTime type. If its want the date to appear in the specified format (01/24/2017), just output it using format() method:

Datetime yourDate =;
String dateOutput = yourDate.format('dd/MM/yyyy');

For Example 3:
January 24, 2017

Datetime myDatetime =;
String myDatetimeStr = myDatetime.format('MMMM d,  yyyy');



Leave a Reply