如何在 iphone 中将 nsstring 更改为 nsdate 格式? [英] How to change nsstring to nsdate format in iphone?
本文介绍了如何在 iphone 中将 nsstring 更改为 nsdate 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在努力将 nsstring 转换为 nsdate 格式.我有一个像 (2011-08-25 18:30:00 +0000) 这样的 nsstring 格式的日期,但我想将它更改为 nsdate 格式 (08/26/2011).如何改变它?任何人都可以帮助我吗?在此先感谢...我花了一天时间来解决这个问题,但我找不到解决方案...请帮助我的朋友....
I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
这是我的代码,
NSString *dateString = @"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(@"Converted date is %@",dateFromString);
输出是,
Converted date is (null)
Yuva.M
推荐答案
NSString *dateWithInitialFormat = @"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(@"dateWithNewFormat: %@", dateWithNewFormat);
来自 NSLog:dateWithNewFormat:08/25/2011
From NSLog: dateWithNewFormat: 08/25/2011
请参阅日期格式模式
这篇关于如何在 iphone 中将 nsstring 更改为 nsdate 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文