1、注意:weekday中1表示周日,2表示周一,7表示周六。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekday | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSLog(@"year = %ld, month = %ld, weekday = %ld, day = %ld, hour = %ld, minure = %ld, second = %ld",
comps.year,comps.month,comps.weekday,comps.day,comps.hour,comps.minute,comps.second);
如果还想获取其它时间信息,可以继续在 unitFlags 中添加类别(如:NSCalendarUnitTimeZone)