在 Objective C 中为 UIDatePicker 设置自定义日期和时间
Setting custom date and time for UIDatePicker in Objective C
我必须为 UIDatePicker 设置自定义日期和时间
最小日期:当前日期时间,
最大日期:到明天晚上。
例如:
那么今天是 3 月 2 日,最大日期应该是 3 月 3 日 11:59 PM
我试过了:
-(NSDate *) getTomorrowDate {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
dayComponent.hour = 23;
dayComponent.minute = 59;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date:[NSDate date] withFormat:@"yyyy-MM-dd hh:mm:ss"] options:0];
return nextDate;
}
但是3月4日也回归了,请指正!
谢谢。
这是一个增加两天,去除时间部分并减去一秒的解决方案。
- (NSDate *)getTomorrowDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
// add two days
NSDate *dayAfterTomorrow = [calendar dateByAddingUnit:NSCalendarUnitDay value:2 toDate:[NSDate date] options:0];
// get start of the day
NSDate *startOfDayAfterTomorrow = [calendar startOfDayForDate:dayAfterTomorrow];
// subtract one second.
return [calendar dateByAddingUnit:NSCalendarUnitSecond value:-1 toDate:startOfDayAfterTomorrow options:0];
}
我必须为 UIDatePicker 设置自定义日期和时间 最小日期:当前日期时间, 最大日期:到明天晚上。
例如: 那么今天是 3 月 2 日,最大日期应该是 3 月 3 日 11:59 PM
我试过了:
-(NSDate *) getTomorrowDate {
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
dayComponent.hour = 23;
dayComponent.minute = 59;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date:[NSDate date] withFormat:@"yyyy-MM-dd hh:mm:ss"] options:0];
return nextDate;
}
但是3月4日也回归了,请指正!
谢谢。
这是一个增加两天,去除时间部分并减去一秒的解决方案。
- (NSDate *)getTomorrowDate {
NSCalendar *calendar = [NSCalendar currentCalendar];
// add two days
NSDate *dayAfterTomorrow = [calendar dateByAddingUnit:NSCalendarUnitDay value:2 toDate:[NSDate date] options:0];
// get start of the day
NSDate *startOfDayAfterTomorrow = [calendar startOfDayForDate:dayAfterTomorrow];
// subtract one second.
return [calendar dateByAddingUnit:NSCalendarUnitSecond value:-1 toDate:startOfDayAfterTomorrow options:0];
}