NSString 到 NSDate
我通过使用以下方法获得了一个包含当前日期的字符串:
NSString *date = [[NSDate date] description];
在不同的点上,我想从此字符串中检索日期,并使用了以下代码:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"YYYY-MM-DD HH:MM:SS ±HHMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];
我将 dateFromString
获取为 nil< /代码> 0x0。我做错了什么?
I got a string that contains the current date by using this :
NSString *date = [[NSDate date] description];
At a different point I want to retrieve the date from this string and I used the following code:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"YYYY-MM-DD HH:MM:SS ±HHMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:<NSString containing date>];
I am getting dateFromString
as nil
0x0. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不能发明格式字符串语法并期望它能够工作;您需要实际使用记录的格式。 (说真的,“MM”同时表示“月”、“分钟”和“GMT 偏移分钟”?)
正如 文档 指出,10.4 格式化程序使用 Unicode 格式字符串。
请尝试使用“yyyy-MM-dd HH:mm:ss ZZZ”。
另外,Objective-C 源代码是 ASCII。不要将像 ± 这样的字符放在那里并期望它在任何上下文中都能工作;相反,使用字符串文件。
You can't invent format string syntax and expect it to work; you need to actually use a documented format. (Seriously, "MM" meaning "month", "minute" and "GMT offset minutes" all at the same time?)
As the documentation points out, the 10.4 formatters use Unicode format strings.
Try "yyyy-MM-dd HH:mm:ss ZZZ" instead.
Also, Objective-C source is ASCII. Don't put characters like ± in there and expect it to work in any context; instead, use strings files.
另外, dateFromString: 返回一个自动释放的 NSDate,所以:
您可能想要:
希望这可以拯救某人:)
Also, dateFromString: returns an autoreleased NSDate, so:
You may want to:
Hope this could save someone :)
为此制作了一个 NSString 扩展。
感谢 NSDataDetector,它识别了一大堆格式。
eppz!kit 的一部分,获取类别 NSString+EPPZKit.h 来自 GitHub。
原始答案:无论您不确定(或者只是不关心)字符串中包含的日期格式,使用 NSDataDetector 来解析日期。
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.
ORIGINAL ANSWER: Whether you're not sure (or just don't care) about the date format contained in the string, use NSDataDetector for parsing date.
您可以查看 TouchTime。
https://github.com/jheising/TouchTime。
它是 PHP 5.4 中很棒的 strtotime 函数的直接移植,适用于 Cocoa 和 iOS。它将接受几乎任何任意格式的日期或时间字符串并将其转换为 NSDate。
希望它有效,并享受!
You might check out TouchTime.
https://github.com/jheising/TouchTime.
It's a direct port of the awesome strtotime function in PHP in 5.4 for Cocoa and iOS. It will take in pretty much any arbitrary format of date or time string and convert it to an NSDate.
Hope it works, and enjoy!
正如前面所回答的,这就是当您发明新语法时会发生的情况。
基于 Apple NSDateFormatter 类参考 文档
这对于输出特别重要,因为每个区域设置的输出都不同。默认情况下,NSDateFormatter 会观察当前用户的区域设置。因此,相同的 NSDate 可能是
22.11.2011 18:33:19
、Nov 22, 2011 6:33:19 PM
或2011-11-22下午 6:33:19
甚至२२-११-२०११ ६:३३:१९ अपराह्
,全部用于相同的输入和相同的代码。例如,考虑此代码
检查 Apple 文档以了解 NSDateFormatterStyle 或这个 优秀教程
As previously answered, that is what happens when you invent a new sytax.
Based on Apple NSDateFormatter Class Reference documentation
This is specially important for the output, which is different for every locale. By default NSDateFormatter observes the current user’s locale settings. So the same NSDate could be
22.11.2011 18:33:19
, orNov 22, 2011 6:33:19 PM
, or2011-11-22 下午6:33:19
or even२२-११-२०११ ६:३३:१९ अपराह्
, all for the same input and with the same code.Consider for example this code
Check Apple docs for the different styles of NSDateFormatterStyle or this excellent tutorial