将NSString转换为NSDate [英] Converting a NSString into NSDate

查看:76
本文介绍了将NSString转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有日期字符串:2010年10月11日星期一。如何从中创建一个NSDate对象,然后从中获取不同的组件,如日,月,日,年。请注意,此字符串的格式/区域设置可能会在运行时更改。

I have date string: Monday, October 11, 2010. How can I create a NSDate object out of it and then get different components like day, month, date, year from it. Please note that format/locale of this string may change at runtime.

推荐答案

另一种可能性:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [calendar components:units fromDate:date];
NSInteger year = [components year];
...

这篇关于将NSString转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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