Converting an ISO 8601 timestamp into an NSDate: H

2019-01-02 15:49发布

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?

标签: nsdate utc iso
5条回答
看淡一切
2楼-- · 2019-01-02 16:23

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"

查看更多
牵手、夕阳
3楼-- · 2019-01-02 16:33

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
查看更多
只靠听说
4楼-- · 2019-01-02 16:39

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);
查看更多
大哥的爱人
5楼-- · 2019-01-02 16:43

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 !
}
查看更多
其实,你不懂
6楼-- · 2019-01-02 16:45

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];
}
查看更多
登录 后发表回答