Convert NSDate to other format [duplicate]

我是研究僧i 提交于 2021-02-07 17:14:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!