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.)
当然,如果您使用不同的日历或日期,则可以轻松更改.可用日历和日历单元的列表可以在 NSCalendar
类参考.关于 NSDateComponents
的更多信息可以在 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
s 提供该信息).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屋!