• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSDate from xsd:dateTime / ISO 8601
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDate from xsd:dateTime / ISO 8601


  • Subject: Re: NSDate from xsd:dateTime / ISO 8601
  • From: Jens Alfke <email@hidden>
  • Date: Mon, 17 Mar 2008 15:36:52 -0700


On 17 Mar '08, at 3:20 PM, Nathan Vander Wilt wrote:

Does -dateWithString: really not support the
"international [standard!] string representation" of
ISO 8601?? What's the right way to convert such an
xsd:dateTime to an NSDate?

+[NSDate dateWithString:] is, I think, configured to recognize one particular date format, which depends on your system locale. It's not very useful. What you want to do is customize an NSDateFormatter object to recognize your format, and then parse using it.


Here's some code I wrote recently. This sets up the date formatter, which only needs to be done once:

    static NSDateFormatter *sISO8601 = [[NSDateFormatter alloc] init];
...
    sISO8601.timeStyle = NSDateFormatterFullStyle;
    sISO8601.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ";

Then you parse a date from a string like:

    NSDate *date = [sISO8601 dateFromString: str];

Actually, looking at my code, there's a gotcha where the NSDateFormatter doesn't seem to understand the "Z" suffix. The best workaround I could find was this kludge:
// Formatters don't undnerstand "Z" timezone, so replace it with "GMT":
if( [str hasSuffix: @"Z"] )
str = [[str substringToIndex: str.length-1] stringByAppendingString: @"GMT"];
Do that just before the -dateFromString call.


—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSDate from xsd:dateTime / ISO 8601
      • From: Nathan Vander Wilt <email@hidden>
References: 
 >NSDate from xsd:dateTime / ISO 8601 (From: Nathan Vander Wilt <email@hidden>)

  • Prev by Date: Re: make a window not draggable in spaces
  • Next by Date: How to track all keyboard event?
  • Previous by thread: NSDate from xsd:dateTime / ISO 8601
  • Next by thread: Re: NSDate from xsd:dateTime / ISO 8601
  • Index(es):
    • Date
    • Thread