开发者

Tell what day it is using NSDate

开发者 https://www.devze.com 2022-12-16 12:45 出处:网络
I have an NSDate and I want to tell if it occurs on a particular day of any year. How do I do that? For instance, I want to see if NSDate\'s day and month are 25 and 12.

I have an NSDate and I want to tell if it occurs on a particular day of any year. How do I do that?

For instance, I want to see if NSDate's day and month are 25 and 12.

I want to go:

NSDate *today = [NSDate date];    
int day = [today day];
int month开发者_Python百科 = [today month];

Is there a way of doing that?


You can use -[NSCalendar components:fromDate:] to do this:

NSDate                     *date = [NSDate date];
NSCalendar                 *cal = [NSCalendar currentCalendar];
int                        comp = NSMonthCalendarUnit |  NSDayCalendarUnit;
NSDateComponents           *cmpnts = [cal components: comp fromDate: date];

int                        day = [cmpnts day];
int                        month = [cmpnts month];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号