开发者

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

开发者 https://www.devze.com 2023-02-15 03:38 出处:网络
I\'m having trouble converting an ISO 8601 timestamp into an NSDate.I tried to use NSDateFormatter, but I can\'t get it to work with the UTC time offset that appears on the end of the timestamps.To ex

I'm having trouble converting an ISO 8601 timestamp into an NSDate. I tried to use NSDateFormatter, but I can't get it to work with the UTC time offset that appears on the end of the timestamps. To explain, I would like to convert a timestamp such as the following into an NSDate开发者_开发技巧: 2011-03-03T06:00:00-06:00. My question is: How do I deal with the "-06:00" part? I tried using yyyy-MM-dd'T'HH:mm:ssZ as my date format string but it doesn't work. Any suggestions?


No need to remove the :'s. To handle the "00:00" style timezone, you just need "ZZZZ":

Swift

let dateString = "2014-07-06T07:59:00Z"

let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ"
dateFormatter.dateFromString(dateString)

Objective-C

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSString *input = @"2013-05-08T19:03:53+00:00";
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZ"]; //iso 8601 format
NSDate *output = [dateFormat dateFromString:input];
NSLog(@"Date output: %@", output);


In my case I received something like that:

"2015-05-07T16:16:47.054403Z"

And I had to use:

"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZ"


The problem is the : character inside the timezone offset. You could explicitly remove just that colon, or remove all of the colons, and then proceed. For example:

NSString *s = @"2011-03-03T06:00:00-06:00";
s = [s stringByReplacingOccurrencesOfString:@":" withString:@""];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HHmmssZZ"];
NSDate *d = [f dateFromString:s];
NSLog(@"%@", d);

This logs:

EmptyFoundation[5088:707] 2011-03-03 12:00:00 +0000


Here is the method that I use:

-(NSDate *)dateFromISO8601String:(NSString *)dateString{
    if (!dateString) return nil;
    if ([dateString hasSuffix:@"Z"]) {
      dateString = [[dateString substringToIndex:(dateString.length-1)] stringByAppendingString:@"-0000"];
    }
    dateString = [dateString stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HHmmssZ";
    return [dateFormatter dateFromString:dateString];
}


Converting ISO 8601 timestamp into NSDate In Swift:

let dateFormatter = NSDateFormatter()
let inputDate = "2015-06-18T19:00:53-07:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZ" //iso 8601

let outputDate = dateFormatter.dateFromString(inputDate)
println(outputDate!) //optional implicitly

Or optional explicitly:

if let outputDate = dateFormatter.dateFromString(inputDate) {
     println(outputDate) //no need !
}


Apple supports a separate format for ISO 8601 format

Objective C

NSISO8601DateFormatter *formater = [[NSISO8601DateFormatter alloc]init];
NSString *string = [formater stringFromDate:[NSDate date]];
0

精彩评论

暂无评论...
验证码 换一张
取 消