iOS--分别获取当前时间年月日时分秒

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    NSLog(@"%ld年",(long)[comps year]);
    NSLog(@"%ld月",(long)[comps month]);
    NSLog(@"%ld日",(long)[comps day]);
    NSLog(@"%ld时",(long)[comps hour]);
    NSLog(@"%ld分",(long)[comps minute]);
    NSLog(@"%ld秒",(long)[comps second]);
    
    
    
    //特殊标注:星期是以周日开始的,1代表周日,2代表周一。。。7代表周六
    NSArray * weekdayNamesArray = [NSArray arrayWithObjects:@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六", nil];
    NSLog(@"%ld",(long)[comps weekday]);
    NSLog(@"%@",[weekdayNamesArray objectAtIndex:[comps weekday] - 1]);
    
    NSLog(@"这个月的第%ld周",(long)[comps weekOfMonth]);
 
    NSLog(@"这一年的第%ld周",(long)[comps weekOfYear]);

你可能感兴趣的:(iOS--分别获取当前时间年月日时分秒)