0

如何将格式 (2013-07-25T06:15:33.180-04:00) 的 NSString 转换为 NSDate?

我正在尝试使用以下代码进行转换

NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init];
    [rfc3339TimestampFormatterWithTimeZone setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
    [rfc3339TimestampFormatterWithTimeZone setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss ZZZ"];

    NSDate *theDate = nil;
    NSError *error = nil;
    if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) {
        NSLog(@"Date '%@' could not be parsed: %@", dateString, error);
    }
    NSString *strDate = [rfc3339TimestampFormatterWithTimeZone stringFromDate:theDate];
    return strDate;

请帮我

4

1 回答 1

2

试试这样,我希望你能成功..

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
    NSDate *date = [dateFormatter dateFromString:@"2013-07-25T06:15:33.180-04:00"];//chnage symbal here.
    NSLog(@"%@",date);
于 2013-07-24T10:30:19.217 回答