简体   繁体   English

如何将NSString转换为NSDate?

[英]How to turn a NSString into 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" “2011-01-13T17:00:00 + 11:00”

into a NSDate? 进入NSDate?

The unicode date format doc is here unicode日期格式doc就在这里

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. 希望这可以帮助。

put the T part in single quotes, and check the unicode docs for the exact formatting. 将T部分放在单引号中,并检查unicode文档以获取确切的格式。 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. 另外,在字符串和nsdates之间来回转换时要小心时区。

Again, in my case, I use: 在我的情况下,我再次使用:

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

Did you try this 你试过这个吗?

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

Cheers 干杯

You might check out TouchTime. 你可以查看TouchTime。

https://github.com/jheising/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. 对于Cocoa和iOS,它是PHP中令人敬畏的strtotime函数的直接端口。 It will take in pretty much any arbitrary format of date or time string and convert it to an NSDate. 它将采用几乎任意格式的日期或时间字符串并将其转换为NSDate。

Hope it works, and enjoy! 希望它有效,并享受!

Try using this cocoapods enabled project. 尝试使用这个启用cocoapods的项目。 There are many added functions that will probably be needed as well. 还可能需要许多附加功能。

"A category to extend Cocoa's NSDate class with some convenience functions." “一个扩展Cocoa的NSDate类的类,带有一些便利功能。”

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

Here's an example from their page: 这是他们页面的一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM