2016-08-01 106 views

回答

2

你可以做這樣的事情,

NSDateFormatter *df = [[NSDateFormatter alloc]init]; 

[df setDateFormat:@"yyyy-MM-dd"]; 

NSDate *currentDate = [NSDate date]; 

NSString *currentDateStr = [[df stringFromDate:currentDate] stringByAppendingString:@" 23:59"]; 

[df setDateFormat:@"yyyy-MM-dd HH:mm"]; 

NSDate *maxDate = [df dateFromString:currentDateStr]; 

NSLog(@"max date : %@",maxDate); 

NSLog(@"max date str : %@",[df stringFromDate:maxDate]); 

yourDatePicker.maximumDate = maxDate; 

更新:

使用NSDateComponents另一種方法通過@droppy的建議,

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]]; 
NSInteger day = [dateComponents day]; 
NSInteger month = [dateComponents month]; 
NSInteger year = [dateComponents year]; 
dateComponents.hour = 23; 
dateComponents.minute = 59; 

NSLog(@"%ld : %ld : %ld",(long)day,(long)month,(long)year); 

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; 
NSDate *resultDate = [gregorianCalendar dateFromComponents:dateComponents]; 

NSDateFormatter *df = [[NSDateFormatter alloc]init]; 
[df setDateFormat:@"yyyy-MM-dd HH:mm"]; 

NSLog(@"result date : %@",resultDate); 
NSLog(@"result date string : %@",[df stringFromDate:resultDate]); 

yourDatePicker.maximumDate = resultDate; 
+1

多麼可怕的方式來做到這一點。 – Droppy

+1

@Droppy:如果你有更好的主意,你可以提出另一種方法! – Lion

+0

是使用'NSDateComponents'。 – Droppy