如何更改存储在字符串中的日期格式?目标c

发布于 2024-12-05 12:21:23 字数 663 浏览 1 评论 0原文

可能的重复:
如何转换 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];

,但它显示空值。这段代码有什么问题?

提前致谢...

Possible Duplicate:
how to convert datetime format in NSString?

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?

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2024-12-12 12:21:23

尽管看起来相同,但它与 @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”

It is different from @Mayur Joshi's answer though it looks to be same.

This is sure to work for you.

 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];

I hope this helps you.

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.

How to convert date string into format "17 Nov 2010"

丶情人眼里出诗心の 2024-12-12 12:21:23
 NSString *dateStr = @"2011-1-24";

 //OR if you have date then declare dateStr like this, 
 NSString *dateStr = [NSString stringWithFormat:@"%@",yourDate];

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"YYYY-M-dd"];
 NSDate *date = [dateFormat dateFromString:dateStr];
 [dateFormat setDateFormat:@"MM-dd-YYYY"];
 NSString* temp = [dateFormat stringFromDate:date];
 [dateFormat release];

 NSLog(@"%@",temp);
 NSString *dateStr = @"2011-1-24";

 //OR if you have date then declare dateStr like this, 
 NSString *dateStr = [NSString stringWithFormat:@"%@",yourDate];

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"YYYY-M-dd"];
 NSDate *date = [dateFormat dateFromString:dateStr];
 [dateFormat setDateFormat:@"MM-dd-YYYY"];
 NSString* temp = [dateFormat stringFromDate:date];
 [dateFormat release];

 NSLog(@"%@",temp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文