Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?

旧城冷巷雨未停 提交于 2019-11-25 21:50:44

You need another formatter to handle the output. Put this after your code:

NSDateFormatter *anotherDateFormatter = [[NSDateFormatter alloc] init];   
[anotherDateFormatter setDateStyle:NSDateFormatterLongStyle];
[anotherDateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"%@", [anotherDateFormatter stringFromDate:myDate]);

I have a similiar but slightly more complex problem, and I've found a very simple solution!

The problem: My incoming ISO8601 dates look like this: 2006-06-14T11:06:00+02:00 They have a timezone offset at the end.

The solution: Use Peter Hosey's ISO8601DateFormatter which you can download from here.

ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSDate *theDate = [formatter dateFromString:dateString];
[formatter release], formatter = nil;

and

ISO8601DateFormatter *formatter = [[ISO8601DateFormatter alloc] init];
NSString *dateString = [formatter stringFromDate:[twitch dateTwitched]];
[formatter release], formatter = nil;
david

Here is sample code from apple

    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    NSDate *date = [formatter dateFromString:dateString];

link:https://developer.apple.com/library/ios/#qa/qa1480/_index.html

Just to mention that since iOS 6.0 you can use this format:

 yyyy-MM-dd'T'HH:mm:ssZZZZZ
Sam Soffes

I have a very fast C implementation of parsing ISO8601 NSStrings to NSDates in SAMCategories. You can install it with CocoaPods or just copy the files to your project.

Here's how to use it:

[NSDate sam_dateFromISO8601String:@"2013-08-24T06:51:21-07:00"]

It's very robust and supports missing timezones, etc. Like my comment above said, NSDateFormatter was very slow for me when parsing lots of dates. Switching to this implementation helped an immense amount.

This code is used in 6 production apps (including Hipstamatic and Roon) that I've worked on with no known issues. There is a test suite to prevent regression.

In short, I think this is the best way to parse ISO8601 strings in Objective-C.


A short aside, if you're concerned about performance and transfer, sending integers instead of ISO8601 strings is greatly preferred. You can simply convert them into dates with [NSDate dateWithTimeIntervalSince1970:yourInteger]. Performance is as fast as you can make it and there are less characters to transfer over the wire.

Colin Wheeler

I should note that last time I checked Apples NSDate methods didn't support ISO8601. I still have a bug with Apple about putting official support in.

+ (id)dateWithNaturalLanguageString:(NSString *)string

will properly (last time I ran it) parse and create an NSDate object from an ISO801 string, though the documentation says you shouldn't use it, it's worked on all ISO8601 dates I've tried so far.

@danielbeard answer worked for me. However you need to include the timezone otherwise it converts a UTC date into your current timezone which isn't what you want.

- (NSDate *) dateFromISO8601DateString:(NSString *) dateString {
    NSDateFormatter * dateFormatter = [NSDateFormatter new];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    NSDate * date = [dateFormatter dateFromString:dateString];
    return date;
}
Andi Utzinger

@Thread captain Try this Format : "yyyy-MM-dd'T'HH:mm:ss.SSS"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!