-1

I want to check if current date is between two given dates. I do the following

 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)

;

Now I have created 2 dates Startdate1 and endDate1. For to check if the current date is between those to dates I am doing this.

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

But I never get a HELL YEAH ??

NOTE: I have set my iPhone datetime to a time between the start and enddate. Also here are my LOGS:

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
Steaphann
  • 2,797
  • 6
  • 50
  • 109

5 Answers5

0

Your logic is flawed. The return value NSOrderedSame means that two values compared equal. That's not what you want. Instead, you want to check if the first date comes before the current date and the second one comes after it:

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

is what you should write.

  • This worked. But i think there is also somehting wrong with the timezones ? I've set that the hour should be 5 but I get 3 ?? – Steaphann May 21 '13 at 09:31
  • 1
    @StefGeelen `NSLog()` doesn't know anything about time zones - if you want proper output, you should use `NSDateFormatter`. –  May 21 '13 at 09:32
0

change your code to

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

This might solve your problem

Happy coding :)

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
-1

You can check this using this :

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

If it returns YES then yourDate lies between the startDate and endDate else it returns NO.

Hope it helps you

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
-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");

}
Mutawe
  • 6,464
  • 3
  • 47
  • 90
-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
}
manujmv
  • 6,450
  • 1
  • 22
  • 35