我有两个日期选择器,它们给了我一个 startTime 和一个 endTime。根据此代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);
在我的.h
@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;
我有这段代码,可以让我比较给定时间和当前时间。什么是最好的测试方法,因为我需要你在后台使用它。
if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime");
}
if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime");
}
我需要在该代码或此代码之间进行选择。所以在这里可以NSLog
工作吗?
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
//start time greater than today
}
else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
//start time less than today
}
else
{
//both dates are equal
}
谢谢