What's the right way to parse an ISO8601 date in cocoa?

前端 未结 1 743
谎友^
谎友^ 2020-12-29 14:46

I would like to parse ISO8601 dates in Cocoa, both for iOS 4+ and OSX 10.6+

There are a few questions about this on StackOverflow already, but in my opinion n

相关标签:
1条回答
  • 2020-12-29 15:42

    The best way is this library. ☺

    I should add a link on that page to the Bitbucket repo, which contains newer source code (including 32-bit and Clang fixes!) and has an issue tracker. If you find any other bugs in it, please file them.

    I'd also like to know what you mean by “more complicated than necessary”. Normal usage is very simple:

    ISO8601DateFormatter *formatter = [[[ISO8601DateFormatter alloc] init] autorelease]; //Or, if you prefer, create it once in -init and own it until -dealloc 
    NSDate *parsedDate = [formatter dateFromString:inString];
    NSString *unparsedString = [formatter stringFromDate:inDate];
    

    You can switch out dateFromString: or stringFromDate: for one of the longer methods if you need more information (e.g., to preserve the time zone).

    If you mean something else, I want to hear it so I can improve the library.

    0 讨论(0)
提交回复
热议问题