NSDate的NSDateComponents [英] NSDateComponents of an NSDate

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

问题描述

如何获取单个NSDate的NSDateComponents?我不想要两个日期之间的差异,只是NSDate的秒,分,小时,日,月和年的差异?

How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate?

推荐答案

Apple docs ,清单3:获取日期组件:

There's an example in the Apple docs, Listing 3: Getting a date’s components:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
                    [gregorian components:(NSDayCalendarUnit | 
                                           NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];

请注意,您必须将日历单位 组件方法。

Note that you have to 'or' together the calendar units you want in the call of the components method.

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

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