检查当前日期是否在两个给定日期之间

时间:2013-05-21 09:19:58

标签: iphone ios objective-c nsdate nsdateformatter

我想检查当前日期是否在两个给定日期之间。我做了以下

 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! ");
    }

但我从未得到过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

5 个答案:

答案 0 :(得分:0)

你的逻辑是有缺陷的。返回值NSOrderedSame表示两个值相等。那不是你想要的。相反,您要检查第一个日期是在当前日期之前,第二个日期在它之后:

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

是你应该写的。

答案 1 :(得分:0)

将您的代码更改为

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

这可能会解决您的问题

快乐编码:)

答案 2 :(得分:-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

希望它可以帮到你

答案 3 :(得分:-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");

}

答案 4 :(得分:-1)

您可以使用以下代码。

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
}