无法使用NSDateFormatter格式化UTC日期

时间:2011-10-20 01:52:59

标签: iphone objective-c cocoa-touch nsdate nsdateformatter

我有以下日期:

2011-10-20T01:10:50Z

我希望将其格式化为

"M/d/yy 'at' h:mma"

这是我的代码:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    //The Z at the end of your string represents Zulu which is UTC
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-dd-MM'T'HH:mm:ss'Z'"];
    NSDate* newTime = [dateFormatter dateFromString:[message valueForKey:@"created_at"]];

    //Add the following line to display the time in the local time zone
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
    NSString* finalTime = [dateFormatter stringFromDate:newTime];
    [dateFormatter release];
    NSLog(@"%@", finalTime);    

不幸的是,finalTime在这里是NULL。

3 个答案:

答案 0 :(得分:34)

你有dd-MM倒退。你有10个dd和20个MM。没有20个月。

2011-10-20T01:10:50Z
@"yyyy-dd-MM'T'HH:mm:ss'Z'"

因此,newTime为null,finalTime为null。

以下是修复方法。这样:

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

NSDate* newTime = [dateFormatter dateFromString:@"2011-10-20T01:10:50Z"];
NSLog(@"original time: %@", newTime);

//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:@"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(@"%@", finalTime);    

[dateFormatter release];

<强>输出:

2011-10-19 22:05:15.107 Craplet[4231:707] original time: 2011-10-20 01:10:50 +0000
2011-10-19 22:05:15.116 Craplet[4231:707] 10/19/11 at 9:10PM

答案 1 :(得分:14)

本技术说明帮助我解决了同样的问题:https://developer.apple.com/library/ios/qa/qa1480/_index.html

简而言之,您需要在NSDateFormatter上设置3个内容:

  1. 区域设置
  2. 时区
  3. 的DateFormat
  4. 代码示例:

    NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:locale];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'"];
    
    NSDate *date = [dateFormatter dateFromString:@"2014-10-23T01:10:50.000Z"];
    

    我正在使用NSDateFormatter通过仅设置时区和日期格式成功地将UTC字符串解析为NSDate。当它突然停止工作时,我发现了这个技术说明,并添加了setLocal:这解决了我的问题。

答案 2 :(得分:4)

因为Swift:

  func createHeartRateStreamingQuery(workoutStartDate: NSDate) -> HKQuery? {
    // adding predicate will not work
    //let predicate = HKQuery.predicateForSamplesWithStartDate(workoutStartDate, endDate: nil, options: HKQueryOptions.None)

    guard let quantityType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate) else { return nil }

    let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -> Void in
        guard let newAnchor = newAnchor else {return}
        self.anchor = newAnchor
        self.updateHeartRate(sampleObjects)