我有一个NSDate,我想告诉它是否发生在任何一年的某一天。我该怎么做?
例如,我想知道NSDate的日期和月份是25和12。
我想去:
NSDate *today = [NSDate date];
int day = [today day];
int month = [today month];
有没有办法做到这一点?
答案 0 :(得分:4)
您可以使用 - [NSCalendar components:fromDate:]来执行此操作:
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];