取得NSDate实例各个字段的数字
代码:
1 NSDate *now = [NSDate date]; 2 NSCalendar *calendar = [NSCalendar currentCalendar]; 3 4 //获取年、月、日、时、分、秒等组件 5 NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:now]; 6 NSLog(@"[%d]", dateComponents.year); //年 7 NSLog(@"[%d]", dateComponents.month); //月 8 NSLog(@"[%d]", dateComponents.day); //日 9 NSLog(@"[%d]", dateComponents.hour); //时 10 NSLog(@"[%d]", dateComponents.minute); //分 11 NSLog(@"[%d]", dateComponents.second); //秒