可能的重复:
如何转换 NSString 中的日期时间格式? https://stackoverflow.com/questions/2076417/how-to-convert-datetime-format-in-nsstring
我已将 json 解析的日期存储在字符串中。日期格式为 2011-1-24。现在我想转换成 MM-dd-YYYY 格式。为此,我正在使用这段代码
NSString *stringDate =[NSString stringWithFormat:@"%@",[list_date objectAtIndex:indexPath.row]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
NSDate *date = [dateFormatter dateFromString:stringDate];
NSString *newDate = [dateFormatter stringFromDate:date];
但它显示空值。这段代码有什么问题?
提前致谢...
尽管看起来相同,但它与 @Mayur Joshi 的答案不同。
这肯定对你有用。
NSString *dateString = @"2011-09-19";
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [format dateFromString:dateString];
[format setDateFormat:@"MM-dd-yyyy"];
NSString* finalDateString = [format stringFromDate:date];
[format release];
我希望这可以帮助你。
如果您需要任何帮助,请在下面发表评论。
您也可以参考此链接以获得更多帮助。该链接与您想要的非常相似,只是目标日期格式不同。
如何将日期字符串转换为格式“17 Nov 2010” https://stackoverflow.com/questions/4203627/how-to-convert-date-string-into-format-17-nov-2010
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)