如何将NSString转换为NSDate?

jen*_*nas 5 xcode objective-c nsdate nsstring nsdateformatter

我一直绞尽脑汁没有运气.有人可以告诉我如何转换这个字符串:

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

进入NSDate?

tbo*_*one 20

unicode日期格式doc就在这里

另外,根据您的情况,您可以尝试这样做:

// 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];
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.