13

I want to compare two NSDates with NOW ([NSDate date]).

NSDate *date1 = [NSDate dateWithString:@"1982-02-12 07:00:00 +0100"];
NSDate *now   = [NSDate dateWithString:@"2012-01-25 10:19:00 +0100"]; //example
NSDate *date2 = [NSDate dateWithString:@"1989-02-12 15:00:00 +0100"];

I would like to check if now is between date1 and date2. In the example above this is the case. The date component should be completely ignored, so only the time component should be compared. How could I accomplish this?

Thanks in advance!

Rens
  • 269
  • 3
  • 11
  • Duplicate of duplicate: http://stackoverflow.com/questions/8276187/determine-if-todays-date-is-in-a-range-of-two-dates-on-ios – Yama Jan 25 '12 at 09:25
  • possible duplicate of [Comparing time in NSDate](http://stackoverflow.com/questions/3773279/comparing-time-in-nsdate) – Sergey Kalinichenko Jan 25 '12 at 09:29

3 Answers3

28
unsigned int flags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSCalendar* calendar = [NSCalendar currentCalendar];

NSDateComponents* components = [calendar components:flags fromDate:date1];

NSDate* timeOnly = [calendar dateFromComponents:components];

This will give you a date object where everything but the hours/minutes/seconds have been reset to some common value. Then you can use the standard NSDate compare functions on them.

For reference, here is the opposite question to yours: Comparing two NSDates and ignoring the time component

Community
  • 1
  • 1
UIAdam
  • 5,303
  • 1
  • 27
  • 23
  • Thank you Adam. There's only one thing: after all the calculations the time components are also changed! The times are subtracted by 20 minutes. How is this possible? – Rens Jan 25 '12 at 09:57
  • 1
    Curious, 20 min is an odd offset... This *might* be something to do with calendar changes since 1/1/1. I tried it myself and though the displayed time stayed the same the offset from GMT (e.g. the timezone) changed. Try adding `[components setYear:1980];` after `NSDateComponents *components = ...` and check the effect (there have been no calendar changes since 1980, I picked the year more-or-less randomly otherwise). For me that removed the GMT offset discrepancy. – CRD Jan 25 '12 at 16:28
  • Thanks but,how to compare a time that begins at 8:00 pm and ends at 1:00 am without using the date? – Filippo Jun 15 '15 at 20:09
1

You can create a date representing the start of today and add the time as components to it to get the boundary dates.

NSDate *now = [NSDate date];
NSDate *startOfToday;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startOfToday interval:NULL forDate:now];


NSDateComponents *startComps = [[NSDateComponents alloc] init];
startComps.hour = 7;
startComps.minute = 30;

NSDateComponents *endComps = [[NSDateComponents alloc] init];
endComps.hour = 20;

NSDate *startDate =  [[NSCalendar currentCalendar] dateByAddingComponents:startComps toDate:startOfToday options:0];
NSDate *endDate =  [[NSCalendar currentCalendar] dateByAddingComponents:endComps toDate:startOfToday options:0];

if ([startDate timeIntervalSince1970] < [now timeIntervalSince1970] && [now timeIntervalSince1970]  < [endDate timeIntervalSince1970]) {
    NSLog(@"good");
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • hey this is great I am going to be reusing this code, how would I make a function of it I want to pass in the startComps.hour/ minute and end.hour/minute and then use the if statement outside the function I was trying this `-(void)getStartTime:(NSDateComponents*)startComps getEndTime:(NSDateComponents*)endComps { NSDate *now = [NSDate date]; NSDate *startOfToday; [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit startDate:&startOfToday interval:NULL forDate:now];` but it wasn't working? –  Oct 21 '14 at 02:49
  • I have added full code in old question here http://stackoverflow.com/questions/26476231/objective-c-compare-two-times –  Oct 21 '14 at 02:57
-2
    NSDateFormatter* formatterDate = [[[NSDateFormatter alloc] init] autorelease]; 

    formatterDate.dateStyle = NSDateFormatterMediumStyle;  // whatever format you like  

    NSDate *first_Date  = [formatterDate dateFromString:@""];
    NSDate  *second_Date  = [formatterDate dateFromString:@""];
    NSDate  *todaysDate =  [NSDate date];


    NSTimeInterval timeIntFormFirstDate = [todaysDate timeIntervalSinceDate:First_Date];
    NSTimeInterval timeIntFronLastDate  = [second_Date timeIntervalSinceDate:todaysDate];

    int interval1 = timeIntFormFirstDate/60;
    int interval2 = timeIntFronLastDate/60;

    if (interval1 >0 && interval2 >0)
    {
        NSLog(@"Today's date is between first and second date");
    }
Naina Soni
  • 720
  • 5
  • 21