通过Cocoa的NSDate解析不受支持的日期格式 [英] Parsing unsupported date formats in via Cocoa's NSDate

查看:84
本文介绍了通过Cocoa的NSDate解析不受支持的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cocoa框架,如何将@2008-12-29T00:27:42-08:00解析为NSDate对象?标准的 -dateWithString:不喜欢。

解决方案

你可以使用 NSDateFormatter 解析日期:

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

unicode日期格式模式定义您可以用 setDateFormat:方法使用的格式字符串。



请注意,如果您要定位10.4,则需要调用: [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; 。不需要iphone或豹,因为这种模式是默认的。


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.

解决方案

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.

这篇关于通过Cocoa的NSDate解析不受支持的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆