Parsing unsupported date formats in via Cocoa's NSDate

前端 未结 4 1702
轮回少年
轮回少年 2020-12-09 05:46

With the Cocoa framework how can I parse @\"2008-12-29T00:27:42-08:00\" into an NSDate object? The standard -dateWithString: doesn\'t like it.

4条回答
  •  悲哀的现实
    2020-12-09 06:12

    You can use NSDateFormatter to parse dates:

        NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
        date = [dateFormatter dateFromString:dateStr];
    

    The unicode date format patterns defines the format string you can use with the setDateFormat: method.

    Note that if you're targeting 10.4 then you need to call: [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];. Not needed for iphone, or leopard as this mode is the default there.

提交回复
热议问题