2017-12-03 6 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]; 
関連する問題