如何更改存储在字符串中的日期格式?目标c [英] How change the date format which is stored in string? Objective c
问题描述
可能重复:
如何在NSString中转换日期时间格式?
<我已将日期存储在json解析的字符串中。日期格式为2011-1-24。现在我想转换为MM-dd-YYYY格式。为此,我正在使用此代码
I have stored date in string from json parsing. The format of date is 2011-1-24. Now i want to convert into MM-dd-YYYY format. For that i am using this code
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];
但它显示为空值。这段代码是什么问题?
But it show null value. What is problem in this code?
预先感谢...
推荐答案
尽管看起来相同,但它与@Mayur Joshi的答案不同。
It is different from @Mayur Joshi's answer though it looks to be same.
这肯定对您有用。
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];
希望对您有所帮助。
如果您需要任何帮助,请在下面留下评论。
If you need any help on this then please leave a comment below.
也可以参考此链接以获取更多帮助。此链接确实与您想要的链接相似,只是目标日期格式不同。
Also you can refer to this link for more help. This link is really similar to what you want, only the target date Format is different.
这篇关于如何更改存储在字符串中的日期格式?目标c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!