如何在今日23:59 pm設置最大日期UIDatepickerView
。 我試過這樣的如今在今天23:59在uidatepickerview中設置最大日期?
datePicker.maximumDate = [NSDate date];
但它不工作。 我該怎麼做?
如何在今日23:59 pm設置最大日期UIDatepickerView
。 我試過這樣的如今在今天23:59在uidatepickerview中設置最大日期?
datePicker.maximumDate = [NSDate date];
但它不工作。 我該怎麼做?
你可以做這樣的事情,
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;
你可以用'NSDateComponents'創建特定日期。 – Larme
你需要創建一個新的'NSDate'for今天的日期與時間23:59 – rckoenes