如何将 NSString 转换为 NSDate?

发布于 2024-10-12 00:03:38 字数 92 浏览 5 评论 0原文

我绞尽脑汁也没有运气。有人可以告诉我如何将此字符串:

“2011-01-13T17:00:00+11:00”

转换为 NSDate 吗?

Ive been racking my brains with no luck. Could someone please tell me how i would convert this string:

"2011-01-13T17:00:00+11:00"

into a NSDate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

溇涏 2024-10-19 00:03:38

unicode日期格式文档位于此处

另外,根据您的情况,您可以尝试以下操作:

// original string
NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"];

// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(@"Date: %@", dte);

// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(@"DateString: %@", dateString);

[dateFormat release];
    [dateFormat2 release];

希望这个有帮助。

The unicode date format doc is here

Also, for your situation, you could try this:

// original string
NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"];

// convert to date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// ignore +11 and use timezone name instead of seconds from gmt
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'+11:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSDate *dte = [dateFormat dateFromString:str];
NSLog(@"Date: %@", dte);

// back to string
NSDateFormatter *dateFormat2 = [[NSDateFormatter alloc] init];
[dateFormat2 setDateFormat:@"YYYY-MM-dd'T'HH:mm:ssZZZ"];
[dateFormat2 setTimeZone:[NSTimeZone timeZoneWithName:@"Australia/Melbourne"]];
NSString *dateString = [dateFormat2 stringFromDate:dte];
NSLog(@"DateString: %@", dateString);

[dateFormat release];
    [dateFormat2 release];

Hope this helps.

桃扇骨 2024-10-19 00:03:38

将 T 部分放在单引号中,并检查 unicode 文档以了解确切的格式。就我而言,我有类似的东西,我这样做:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSS"];

同样,不完全相同,但你明白了。另外,在字符串和 nsdate 之间来回转换时请注意时区。

同样,就我而言,我使用:

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];

put the T part in single quotes, and check the unicode docs for the exact formatting. In my case, I have something similar, which I do this:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSS"];

Again, not exactly the same, but you get the idea. Also, be careful of the timezones when converting back and forth between strings and nsdates.

Again, in my case, I use:

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"America/New_York"]];
耶耶耶 2024-10-19 00:03:38

你试过这个

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];

干杯吗

Did you try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *dateT = [dateFormatter dateFromString:str];

Cheers

握住你手 2024-10-19 00:03:38

您可以查看 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-10-19 00:03:38

尝试使用这个支持 cocoapods 的项目。可能还需要许多附加功能。

“一个用一些便利功能扩展 Cocoa 的 NSDate 类的类别。”

https://github.com/billymeltdown/nsdate-helper

这是他们页面上的示例:

NSDate *date = [NSDate dateFromString:@"2009-03-01 12:15:23"];

Try using this cocoapods enabled project. There are many added functions that will probably be needed as well.

"A category to extend Cocoa's NSDate class with some convenience functions."

https://github.com/billymeltdown/nsdate-helper

Here's an example from their page:

NSDate *date = [NSDate dateFromString:@"2009-03-01 12:15:23"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文