首页 > 代码库 > 取得NSDate实例各个字段的数字

取得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);  //