【问题标题】:How to convert NSString to NSDate with this format : "2012-08-01T12:43:35+02:00"? [duplicate]如何使用这种格式将 NSString 转换为 NSDate:“2012-08-01T12:43:35+02:00”? [复制]
【发布时间】:2012-08-02 08:30:19
【问题描述】:

我需要将 NSString 像这样:“2012-08-01T12:43:35+02:00”转换为 NSDate,但我找不到好的格式...

我使用这个类别代码将 NSString 转换为 NSDate:

[NSDate dateFromString:@"2012-08-01T12:43:35+02:00" withFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"];

 

+ (NSDate*) dateFromString:(NSString*)dateString withFormat:(NSString*)format 
{
    return [self dateFromString:dateString withFormat:format andLocaleIdentifier:@"fr"];
}

+ (NSDate*) dateFromString:(NSString*)dateString withFormat:(NSString*)format andLocaleIdentifier:(NSString*)localeIdentifier
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
    [dateFormatter setDateFormat:format];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];

    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:localeIdentifier];
    [dateFormatter setLocale:locale];
    [locale release];

    NSDate *returnDate = [dateFormatter dateFromString:dateString];
    [dateFormatter release];

    return returnDate;
}

我尝试了所有格式,但没有一个是正确的......

yyyy-MM-dd'T'HH:mm:ss >> 2012-08-04T18:05:14
yyyy-MM-dd'T'HH: mm:ssv >> 2012-08-04T18:05:14法国时间
yyyy-MM-dd'T'HH:mm:ssvv >> 2012-08-04T18: 05:14GMT+02:00
yyyy-MM-dd'T'HH:mm:ssvvv >> 2012-08-04T18:05:14GMT+02:00
yyyy-MM-dd'T'HH:mm:ssV >> 2012-08-04T18:05:14CEST
yyyy-MM-dd'T'HH:mm:ssVV强> >> 2012-08-04T18:05:14GMT+02:00
yyyy-MM-dd'T'HH:mm:ssVVV >> 2012-08-04T18:05: 14GMT+02:00
yyyy-MM-dd'T'HH:mm:ssVVVV >> 2012-08-04T18:05:14法国时间
yyyy-MM- dd'T'HH:mm:ssz >> 2012-08-04T18:05:14GMT+02:00
yyyy-MM-dd'T'HH:mm:sszz >> 2012-08-04T18:05:14GMT+02:00
yyyy-MM-dd'T'HH:mm:sszzz >> 2012-08-04T18:05:14GMT +02:00
yyyy-MM-dd'T'HH:mm:sszzzz >> 2012-08-04T18:05:14中欧夏令时
yyyy-MM -dd'T'HH:mm:ssZ >> 2012-08-04T18:05:14+0200
yyyy-MM-dd'T'HH:mm:ssZZ >> 2012-08-04T18:05:14+0200
yyyy-MM-dd'T'HH:mm: ssZZZ >> 2012-08-04T18:05:14+0200
yyyy-MM-dd'T'HH:mm:ssZZZZ >> 2012-08-04T18:05 :14GMT+02:00

我该怎么办?


[编辑] 第一个丑陋的解决方案:

  1. 用正则表达式删除最后一个冒号
  2. 用这种格式“yyyy-MM-dd'T'HH:mm:ssZZZ”转换字符串

有正则表达式

    NSString* dateString = @"2012-08-01T12:43:35+02:00";
    NSMutableString* dateWithoutColonString = [dateString mutableCopy];

    NSRegularExpression* lastColonRegex = [NSRegularExpression
                                           regularExpressionWithPattern:@"([\\+\\-][0-9][0-9]):"
                                           options:NSRegularExpressionCaseInsensitive
                                           error:nil];

    [lastColonRegex replaceMatchesInString:dateWithoutColonString
                              options:NSRegularExpressionCaseInsensitive
                                range:NSMakeRange(0, [dateString  length])
                         withTemplate:@"$1"];

【问题讨论】:

标签: objective-c ios date


【解决方案1】:

Apple 讨论了parsing Internet-style dates,它与您的代码类似,但有一些更改。它始终使用en_US_POSIX 语言环境,并且使用[NSTimeZone timeZoneForSecondsFromGMT:0] 而不是您的本地时区进行解析。尝试一下,看看结果是否更适合您。

【讨论】:

  • 感谢您的回复,但如果我没记错的话,它并没有解决我的问题...
  • 它不识别格式并返回nil。
【解决方案2】:

使用[格式化程序setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 2016-08-03
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    相关资源
    最近更新 更多