NSDate获取年/月/日 [英] NSDate get year/month/day
问题描述
在没有其他信息的情况下,如何获取NSDate
对象的年/月/日?我意识到我可以用类似的方法做到这一点:
How can I get the year/month/day of a NSDate
object, given no other information? I realize that I could probably do this with something similar to this:
NSCalendar *cal = [[NSCalendar alloc] init];
NSDateComponents *components = [cal components:0 fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];
但是,对于像获取NSDate
的年/月/日这样的简单操作来说,这似乎很麻烦.还有其他解决方案吗?
But that seems to be a whole lot of hassle for something as simple as getting a NSDate
's year/month/day. Is there any other solution?
推荐答案
因为这显然是我最受欢迎的答案,所以我将尝试对其进行编辑以包含更多信息.
Because this is apparently my most popular answer, I'll try to edit it to contain a little bit more information.
尽管其名称为NSDate
,但其本身仅标记了机器时间点,而不是日期. NSDate
指定的时间点与年,月或日之间没有关联.为此,您必须参考日历.任何给定的时间点都会根据您要查看的日历返回不同的日期信息(例如,公历和犹太历中的日期都不相同),而公历是日历中使用最广泛的日历世界-我假设-我们对NSDate
应该始终使用它有点偏见.幸运的是,NSDate
是两党的.
Despite its name, NSDate
in and of itself simply marks a point in machine time, not a date. There's no correlation between the point in time specified by an NSDate
and a year, month, or day. For that, you have to refer to a calendar. Any given point in time will return different date information based on what calendar you're looking at (dates are not the same in both the Gregorian and Jewish calendars, for instance), and while the Gregorian calendar is the most widely used calendar in the world - I'm assuming - we're a little biased that NSDate
should always use it. NSDate
, luckily, is far more bipartisan.
您必须提到,获取日期和时间必须经过NSCalendar
,但是有一种更简单的方法可以做到:
Getting date and time is going to have to pass through NSCalendar
, as you mentioned, but there's a simpler way to do it:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
这会生成一个NSDateComponents
对象,该对象包含当前日期的当前系统日历中的日,月和年. (注意:不一定是当前的用户指定的日历,只是默认的系统日历.)
That generates an NSDateComponents
object containing the day, month, and year from the current system calendar for the current day. (Note: this isn't necessarily the current user-specified calendar, just the default system one.)
当然,如果您使用其他日历或日期,则可以轻松进行更改.可用日历和日历单位的列表可在NSDateComponents的更多信息. ="noreferrer"> NSDateComponents
类参考.
Of course, if you're using a different calendar or date, you can easily change that. A list of available calendars and calendar units can be found in the NSCalendar
Class Reference. More information about NSDateComponents
can be found in the NSDateComponents
Class Reference.
作为参考,从NSDateComponents
访问单个组件非常容易:
For reference, accessing individual components from the NSDateComponents
is rather easy:
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
您只需要注意:NSDateComponents
不会包含您要查询的任何字段的有效信息,除非您使用该有效信息生成了它们(即请求NSCalendar
为该信息提供NSCalendarUnit
). NSDateComponents
本身不包含任何参考信息-它们只是保存数字供您访问的简单结构.例如,如果您还想从NSDateComponents
中获得一个时代,则必须使用NSCalendarUnitEra
标志从NSCalendar
提供生成器方法.
You just have to be mindful: NSDateComponents
won't contain valid information for any fields you ask for unless you generated them with that valid information (i.e. request NSCalendar
to provide that information with NSCalendarUnit
s). NSDateComponents
contain no reference information in and of themselves - they're just simple structures that hold numbers for you to access. If you want to also get an era, for instance, out of NSDateComponents
, you'll have to feed the generator method from NSCalendar
with the NSCalendarUnitEra
flag.
这篇关于NSDate获取年/月/日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!