Reputation: 589
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?
Upvotes: 2
Views: 8928
Reputation: 8333
try the following code:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
Upvotes: 4
Reputation: 25144
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
Upvotes: 8