NSString 到 NSDate

发布于 2024-08-03 19:30:27 字数 663 浏览 1 评论 0原文

我通过使用以下方法获得了一个包含当前日期的字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春夜浅 2024-08-10 19:30:27

您不能发明格式字符串语法并期望它能够工作;您需要实际使用记录的格式。 (说真的,“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.

海之角 2024-08-10 19:30:27

另外, dateFromString: 返回一个自动释放的 NSDate,所以:

NSDate *dateFromString = [[NSDate alloc] init]; // <- non freed instance
dateFromString = [dateFormatter dateFromString:<NSString containing date>]; // <- new autoreleased instance
....
[dateFromString release]; // <- wrong

您可能想要:

//NSDate *dateFromString = [[NSDate alloc] init];
NSDate *dateFromString = [dateFormatter dateFromString:<NSString containing date>];
....
//[dateFromString release]; // dateFromString is autoreleased

希望这可以拯救某人:)

Also, dateFromString: returns an autoreleased NSDate, so:

NSDate *dateFromString = [[NSDate alloc] init]; // <- non freed instance
dateFromString = [dateFormatter dateFromString:<NSString containing date>]; // <- new autoreleased instance
....
[dateFromString release]; // <- wrong

You may want to:

//NSDate *dateFromString = [[NSDate alloc] init];
NSDate *dateFromString = [dateFormatter dateFromString:<NSString containing date>];
....
//[dateFromString release]; // dateFromString is autoreleased

Hope this could save someone :)

揽月 2024-08-10 19:30:27

为此制作了一个 NSString 扩展

// Simple as this.   
date = dateString.dateValue;

感谢 NSDataDetector,它识别了一大堆格式。

// Tested in GMT+1 (Hungary).
@"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000>
@"20140116" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000>
@"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000>
@"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000>
@"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000>

eppz!kit 的一部分,获取类别 NSString+EPPZKit.h 来自 GitHub。


原始答案:无论您不确定(或者只是不关心)字符串中包含的日期格式,使用 NSDataDetector 来解析日期

//Role players.
NSString *dateString = @"Wed, 03 Jul 2013 02:16:02 -0700";
__block NSDate *detectedDate;

//Detect.
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
[detector enumerateMatchesInString:dateString
                           options:kNilOptions
                             range:NSMakeRange(0, [dateString length])
                        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{ detectedDate = result.date; }];

Made an NSString extension for that.

// Simple as this.   
date = dateString.dateValue;

Thanks to NSDataDetector, it recognizes a whole lot of format.

// Tested in GMT+1 (Hungary).
@"2014-01-16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014.01.16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014/01/16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16" dateValue is <2014-01-16 11:00:00 +0000>
@"2014 Jan 16th" dateValue is <2014-01-16 11:00:00 +0000>
@"20140116" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01.16.2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01/16/2014" dateValue is <2014-01-16 11:00:00 +0000>
@"16 January 2014" dateValue is <2014-01-16 11:00:00 +0000>
@"01-16-2014 17:05:05" dateValue is <2014-01-16 16:05:05 +0000>
@"01-16-2014 T 17:05:05 UTC" dateValue is <2014-01-16 17:05:05 +0000>
@"17:05, 1 January 2014 (UTC)" dateValue is <2014-01-01 16:05:00 +0000>

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.

//Role players.
NSString *dateString = @"Wed, 03 Jul 2013 02:16:02 -0700";
__block NSDate *detectedDate;

//Detect.
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingAllTypes error:nil];
[detector enumerateMatchesInString:dateString
                           options:kNilOptions
                             range:NSMakeRange(0, [dateString length])
                        usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{ detectedDate = result.date; }];
红颜悴 2024-08-10 19:30:27

您可以查看 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!

何时共饮酒 2024-08-10 19:30:27

正如前面所回答的,这就是当您发明新语法时会发生的情况。

基于 Apple NSDateFormatter 类参考 文档

您可以在样式日期上获取和设置许多属性
格式化程序,...
不过,我们建议您不要更改个别设置。相反,您应该接受初始化时建立的默认设置,并使用 setDateStyle:、setTimeStyle:指定格式


这对于输出特别重要,因为每个区域设置的输出都不同。默认情况下,NSDateFormatter 会观察当前用户的区域设置。因此,相同的 NSDate 可能是 22.11.2011 18:33:19Nov 22, 2011 6:33:19 PM2011-11-22下午 6:33:19 甚至 २२-११-२०११ ६:३३:१९ अपराह्,全部用于相同的输入和相同的代码。

例如,考虑此代码

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

// ShortStyle used: 11/23/37 3:30pm
NSString *dateString = [dateFormatter stringFromDate:date];

检查 Apple 文档以了解 NSDateFormatterStyle 或这个 优秀教程

As previously answered, that is what happens when you invent a new sytax.

Based on Apple NSDateFormatter Class Reference documentation

There are many attributes you can get and set on a style date
formatter
, ...
You are encouraged, however, not to change individual settings. Instead you should accept the default settings established on initialization and specify the format using setDateStyle:, setTimeStyle:

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, or Nov 22, 2011 6:33:19 PM, or 2011-11-22 下午6:33:19 or even २२-११-२०११ ६:३३:१९ अपराह्, all for the same input and with the same code.

Consider for example this code

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];

// ShortStyle used: 11/23/37 3:30pm
NSString *dateString = [dateFormatter stringFromDate:date];

Check Apple docs for the different styles of NSDateFormatterStyle or this excellent tutorial

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文