通过我的应用程序,用户可以在餐馆订购特定食品。问题是,如果用户想要订购特定产品,他可以至少给餐厅提供6小时的时间来交付产品。例如,您是用户,现在时间是中午12:00。在应用程序中,用户可以通过UIDatepicker选择交付时间。如果用户选择06:00 pm之前的时间,将生成一个警报视图,消息"请给6小时时间为了你的订单"。我必须比较系统时间和UIDatepickers时间。我找到了一种方法,但它没用。
if ([self.datePicker.date compare:currentdate] == NSOrderedDescending){
NSLog(@"Descending");
}
请帮助我......提前致谢
答案 0 :(得分:0)
使用此
if ([[NSDate date] isEqualToDate: currentdate) {
NSLog(@"currentDate is equal to currentdate");
}
答案 1 :(得分:0)
您可以通过
比较所选时间NSDate *date1 = [NSDate date];
NSDate *date2 = datePicker.date;
NSTimeInterval elapsed = [date1 timeIntervalSinceDate:date2];
此外,您可以在6小时后设置日期选择器的最短时间
NSDate *mydate = [NSDate date];
NSTimeInterval secondsInSixHours = 6 * 60 * 60;
NSDate *dateSixHoursAhead = [mydate dateByAddingTimeInterval:secondsInSixHours];
[datePicker setMinimumDate:dateSixHoursAhead];
答案 2 :(得分:0)
您实际上在寻找时差,这是代码:
NSLog(@"please give %@fhrs time for your order",[self.datePicker.date timeIntervalSinceDate:[NSDate date]]);
答案 3 :(得分:0)
使用此方法。在两个日期之间进行比较的最佳方式:
- (void)pickerDateIsSmallerThanCurrent:(NSDate *)checkDate
{
NSDate* enddate = checkDate;
NSDate* currentdate = [NSDate date];
NSTimeInterval distanceBetweenDates = [enddate timeIntervalSinceDate:currentdate];
double secondsInMinute = 60;
NSInteger secondsBetweenDates = distanceBetweenDates / secondsInMinute;
if (secondsBetweenDates == 0)
//both dates are equal
else if (secondsBetweenDates < 0)
//selected date is smaller than current date
else
//selected date is greater than current date
}