从日期字符串中检索年份和月份
我想从这种日期中检索年份和月份:2011-12-23 10:45:01 但没有运气。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy"];
NSLog(@"Date = %@",[dateFormatter dateFromString:@"2011-12-23 10:45:01"]);
[dateFormatter release];
Date = (null),我不明白为什么。
I want to retrieve year and then month from this kind of date: 2011-12-23 10:45:01 with no luck.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy"];
NSLog(@"Date = %@",[dateFormatter dateFromString:@"2011-12-23 10:45:01"]);
[dateFormatter release];
Date = (null), i can't understand why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须分两步完成,首先匹配整个日期,然后输出你想要的位:
You have to do it in two steps, first match the whole date, then output the bits you want:
这已经在 将 NSDate 格式化为特定样式中得到了回答对于年、月、日和时、分、秒。
具体来说:
This was already answered in Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds.
Specifically: