4

I have date field "dt" which debug like '2015-06-25', and I have to change the format like 06/25/2015. Since we can't use .format('MM/DD/YYYY') on date field. I have converted it to DateTime.

DateTime dtt = datetime.newInstanace (dt.Year(),dt.month().dt.day()) ;

Now when I debug dtt ,it's coming as 2015-06-25 07:00:00 , Don't know how 7 is coming. anyways when I try to change the format..

dtt.format('MM/DD/YYYY') is coming as 06/176/2015 but it should come 06/25/2015

please help . Thanks

4

The format method uses the Java Simple Date Format in which the meaning of D and d are different

D : Day in year

d : Day in month

so if you replace Capital D with small d it should work

0

You already have a date 'as a date', not as a string. The format you are talking about (2015-06-25 07:00:00) is just a visual representation of a value contained in variable of a DateTime type. If you want the date to appear in the specified format (06/25/2015), just output it using format() method:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.