How can convert date into "dd/MM/yyyy" format?

Anki picture Anki · Mar 1, 2012 · Viewed 8.9k times · Source

In my app I am using date picker & show date in textfield,

Date picker given me a date in this format "Mar 1, 2012"

I want that date in "dd/MM/yyyy" format.

how can i do that?

Answer

Cyrille picture Cyrille · Mar 1, 2012

The date picker displays "Mar 1, 2012" but internally it stores the date as a NSDate, which you can convert to a NSString by using a NSDateFormatter, specifying the format you want:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy"];
NSString *formattedDate = [df stringFromDate:yourDatePicker.date];
[df release];

The reference for the format string is here: http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns