问题
Possible Duplicate:
NSDateFormatter and yyyy-MM-dd
I have an NSDate variable in format yyyy-MM-dd that I want to convert to dd/MM/yyyy. How can I do it?
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [f dateFromString:[[jugador objectAtIndex:0] valueForKey:@"FECHA_NAC"]];
Thanks
回答1:
Using same NSDateformatter u can format date using setDateFormat and retreive date using dateFromString
For example :
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"yyyy-MM-dd"];
NSString *strToday = [dateFormat1 stringFromDate:[NSDate date]];// string with yyyy-MM-dd format
[dateFormat1 setDateFormat:@"dd/MM/yyyy"];
NSDate *todaydate = [dateFormat1 dateFromString:strToday];// date with dd/MM/yyyy format
EDIT : changes in code of example
回答2:
After you call dateFromString:
, just set the desired format on the same date formatter and call stringFromDate:
.
来源:https://stackoverflow.com/questions/10901764/convert-nsdate-to-other-format