如何在 iPhone 中将 nsstring 更改为 nsdate 格式?
我一直在努力将 nsstring 转换为 nsdate 格式。我有一个 nsstring 格式的日期,例如 (2011-08-25 18:30:00 +0000),但我想将其更改为 nsdate 格式 (08/26/2011)。如何改变呢?有人可以帮我吗?提前致谢...我花了一天的时间来解决这个问题,但我找不到解决方案..请帮助我的朋友...
这是我的代码,
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
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....
This is my code,
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);
Output is,
Converted date is (null)
Yuva.M
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自 NSLog:dateWithNewFormat:08/25/2011
请参阅日期格式模式
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
您需要使用 NSDateFormatter 并设置日期格式以匹配您的存储方式,如下所示。
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.