-1

我想检查当前日期是否在两个给定日期之间。我执行以下操作

 NSDate *now = [NSDate new];
    NSLog(@"Now is %@",now);
    NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components setYear:2013];
    [components setMonth:06];
    [components setDay:28];
    [components setHour:5];
    [components setMinute:00];
    NSDate *startDate1 = [myCalendar dateFromComponents:components];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone localTimeZone];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSLog(@"start dateeee is %@",startDate1);

    NSDateComponents* components2 = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
    [components2 setYear:2013];
    [components2 setMonth:06];
    [components2 setDay:29];
    [components2 setHour:4];
    [components2 setMinute:59];

    NSDate *endDate1 = [myCalendar dateFromComponents:components];
    NSLog(@"end date is %@",endDate1)

;

现在我创建了 2 个日期Startdate1endDate1. 为了检查当前日期是否在我正在执行的日期之间。

NSComparisonResult result1 = [now compare:startDate1];
    NSComparisonResult result2 = [now compare:endDate1];
if (result2 == NSOrderedSame && result1 == NSOrderedSame ) {
        NSLog(@"HELL YEAH! ");
    }

但我从来没有得到一个地狱耶??

注意:我已将我的 iPhone 日期时间设置为开始日期和结束日期之间的时间。这里还有我的日志:

2013-06-29 11:11:48.727 Genk on stage[2205:907] Now is 2013-06-29 09:11:48 +0000
2013-06-29 11:11:48.728 Genk on stage[2205:907] start dateeee is 2013-06-28 03:00:00 +0000
2013-06-29 11:11:48.729 Genk on stage[2205:907] end date is 2013-06-28 03:00:00 +0000
4

5 回答 5

0

将您的代码更改为

if (result2 == NSOrderedDescending && result1 == NSOrderedAscending ) {
        NSLog(@"HELL YEAH! ");
    }

这可能会解决您的问题

快乐编码:)

于 2013-05-21T09:26:54.180 回答
0

你的逻辑有缺陷。返回值NSOrderedSame意味着两个值比较相等。那不是你想要的。相反,您想检查第一个日期是否在当前日期之前,第二个日期是否在其之后:

if (result1 == NSOrderedAscending && result2 == NSOrderedDescending)

是你应该写的。

于 2013-05-21T09:24:56.860 回答
-1

You can use the following code.

NSTimeInterval fromTime = [startDate1 timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [endDate1 timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];
if(currTime>fromTime && currTime<toTime)
{
      //current time lies between the two dates
}
于 2013-05-21T09:29:21.573 回答
-1

您可以使用以下方法进行检查:

- (BOOL)yourDate:(NSDate *)date inRangeFirstDate:(NSDate *)startDate lastDate:(NSDate *)endDate {
   return [date compare:startDate] == NSOrderedDescending &&
          [date compare:endDate]  == NSOrderedAscending;
}

如果返回YES,则 yourDate 位于 startDate 和 endDate 之间,否则返回NO

希望对你有帮助

于 2013-05-21T09:24:10.123 回答
-1
NSDate * now;.....
NSDate * endDate1;......

if ([now compare: endDate1] == NSOrderedDescending) {
    NSLog(@"now is later than endDate1");        

} else if ([now compare: endDate1] == NSOrderedAscending) {
    NSLog(@"now is earlier than endDate1");

} else {
    NSLog(@"dates are the same");

}
于 2013-05-21T09:24:28.827 回答