2017-12-03 7 views
0

NSDateComponents를 사용하여 매일, 월, 일,시, 분 등을 개별적으로 얻지 만 일, 월, 년 및 초가 2017-11-08로 정확하지 않습니다. 1:00:00 .... 내가 누락 된 것이 누구인지 아는 사람 있습니까?NSDateComponents가 정확한 일/월/년 시간을 표시하지 않습니다.

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; 
     NSDate *date = [dateFormatter dateFromString:@"2017-11-08 1:00:00"]; 
     NSCalendar *calendar = [NSCalendar currentCalendar]; 
     NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date]; 
     NSInteger day = [components day]; 
     NSInteger month = [components month]; 
     NSInteger year = [components year]; 
     NSInteger hour = [components hour]; 
     NSInteger minute = [components minute]; 
     NSInteger second = [components second]; 
     NSLog(@"day: %ld",(long)day); 
     NSLog(@"month: %ld",(long)month); 
     NSLog(@"year: %ld",(long)year); 
     NSLog(@"hour: %ld",(long)hour); 
     NSLog(@"minute: %ld",(long)minute); 
     NSLog(@"second: %ld",(long)second); 

출력 : 그건 당신이 요구하는 모든 때문에

 day: 2147483647 
     month: 2147483647 
     year: 2147483647 
     hour: 1 
     minute: 0 
     second: 2147483647 

답변

2

당신은, 시간과 분을 받고 있습니다. 일, 달, 년, 초를 실제로 묻는다면 다음과 같이 입력하면됩니다.

NSCalendarUnit units = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | 
         NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 
NSDateComponents *components = [calendar components:units fromDate:date];