+ Start a Discussion
Sachin10Sachin10 

Convert Date to DD-MM-YYYY

Hi,
I would like to convert Date to String in  DD-MM-YYYY format  in my controller Class

I can easily convert it using format method if it is DateTime but not Date.

If I use the corresponding Date functions I'm not getting the format directly
For Eg:
date todayDate = system.today();
Now if I use todayDate.day()  I get 4 not 04.
So again I need to convert this into DD format.

Is there any easy approach to convert Date to String in DD-MM-YYYY format?
Many thanks in advance!!


Best Answer chosen by Sachin10
MikeGillMikeGill
Time is irrelevant becuase you are formatting - so why does it matter?

Even if you don't have time cast your date into datetime, then format the way you want.

All Answers

MikeGillMikeGill
Hi,

Try this example in execute anonymous to test

DateTime yourDate = Datetime.now();
String dateOutput = yourDate.format('dd-MM-yyyy');

System.debug('Debug: '+dateOutput);

Hope this helps
Sachin10Sachin10
@ Mike,
Thanks for your reply.
As I mentioned before, I knew how to do it when it is a Date Time,but the scenarion I have is a Date field.
NOT DATETIME.
So let me know if you know anything related to the same.

Thanks!
MikeGillMikeGill
Time is irrelevant becuase you are formatting - so why does it matter?

Even if you don't have time cast your date into datetime, then format the way you want.
This was selected as the best answer
Sachin10Sachin10
Thanks a lot Mike:)
It worked.
MikeGillMikeGill
Awesome!