Tell what day it is using NSDate
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];
精彩评论