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)