Search code examples
iphoneobjective-cnsdateformatternsdatepicker

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


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?


Solution

  • 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