将 NSString 格式化为 NSDate
我从 facebook 获取用户日期,看起来像
“生日日期” = “1987 年 3 月 4 日” 我写了这段代码
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
NSDate *dateFromString = [dateFormatter dateFromString:birthDate];
NSLog(@"dateformstring %@ ",dateFromString);
[birthPicker setDate:dateFromString animated:YES];
和字符串 si 的结果日期:1988-12-25 23:00:00 +0000,我在选择器中选择了这个日期,我不知道这个日期在哪里:/
i take the user date from facebook , it look like
"birthday_date" = "03/04/1987"
i wrote this code
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"dd/MM/YYYY"];
NSDate *dateFromString = [dateFormatter dateFromString:birthDate];
NSLog(@"dateformstring %@ ",dateFromString);
[birthPicker setDate:dateFromString animated:YES];
and the result date from string si : 1988-12-25 23:00:00 +0000 , i have this date selected in the picker , I do not know where did this date :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
Try
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
哎呀。正如 tilo 所说,使用“yyyy”。正如 Apple 文档中所述:
“一个常见的错误是使用 YYYY。yyyy 指定日历年,而 YYYY 指定 ISO 中使用的年份(“一年中的周”)年周日历。”
oops. As tilo said, use "yyyy." As mentioned in the Apple docs:
"A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar."
为此制作了一个 NSString 扩展。
感谢 NSDataDetector,它识别了一大堆格式。
eppz!kit 的一部分,获取类别 NSString+EPPZKit.h 来自 GitHub。
Made an NSString extension for that.
Thanks to NSDataDetector, it recognizes a whole lot of format.
Part of eppz!kit, grab the category NSString+EPPZKit.h from GitHub.