-1
NSDate* firstTime = (NSDate *)openTime;//10:00AM
NSDate* secondTime = (NSDate *)closeTime;//10:00PM
NSDate* nowTime = (NSDate *)str;//3:09PM

NSComparisonResult result1 = [nowTime  compare:firstTime];
NSComparisonResult result2 = [nowTime  compare:secondTime];
if(result1==NSOrderedDescending  && result2==NSOrderedAscending)
{
    return TRUE;//------ expecting result is true as per sample
}
else
{
    return FALSE;
}

I am having this code and I am expecting result is true that I gave in code. but I am getting is false. My aim is to check my current time between the start time and end time. Please help me.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
Subin Kurian
  • 283
  • 1
  • 6
  • 13
  • What are the 'date' components of your 'openTime' and 'closeTime'. Try some 'NSLog'-ing. – Rok Jarc Apr 29 '13 at 09:49
  • http://stackoverflow.com/questions/1072848/how-to-check-if-an-nsdate-occurs-between-two-other-nsdates – Ramu Pasupuleti Apr 29 '13 at 09:50
  • both "duplicate" links are linked to date&time comparsion questions - OP (seems so) wants to compare only time-components of `NSDate` – Rok Jarc Apr 29 '13 at 09:53
  • 1
    @SubinKurian: how do you really (in your code) instantiate `openTime` and `closeTime`? – Rok Jarc Apr 29 '13 at 10:00
  • no date components we have.. only time and it not getting worked at any way time coming as a string 10:00AM-10:00PM and i am parsing it to two components and i need is compair with current time. is this current time in between this time or not – Subin Kurian Apr 29 '13 at 10:00
  • Is your openTime, closeTime really instances of NSDate? or are they strings? – Anupdas Apr 29 '13 at 10:16
  • NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]]; [dateFormatter setDateFormat:@"hh:mma"]; NSString *str=[dateFormatter stringFromDate:[NSDate date]]; – Subin Kurian Apr 29 '13 at 10:20
  • Is my answer not working? If not can you share how openTime and closeTime is instantiated? – Anupdas Apr 29 '13 at 12:06

4 Answers4

4

Try

NSString *fromDateString = @"10:00AM";
NSString *toDateString = @"10:00PM";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"hh:mma"];
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];

NSString *nowDateString = [dateFormatter stringFromDate:[NSDate date]];

NSDate *firstTime   = [dateFormatter dateFromString:fromDateString];
NSDate *secondTime  = [dateFormatter dateFromString:toDateString];
NSDate *nowTime     = [dateFormatter dateFromString:nowDateString];

NSComparisonResult result1 = [nowTime compare:firstTime];
NSComparisonResult result2 = [nowTime compare:secondTime];

if ((result1 == NSOrderedDescending) &&
    (result2 == NSOrderedAscending)){
    return TRUE;
}else{
    return FALSE;
}
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • Thanks I would like to know about what will be result if we campare time in 2 days.means comparing time in 2 days eg 4PM and 4AM – Rahul K Rajan Jun 29 '16 at 11:15
0

I tried with some dummy data in OSX, you need to change with your original values

NSDate *nowTime=[NSDate date];
NSDate *firstTime=[NSDate dateWithNaturalLanguageString:@"22 April 2013"];
NSDate *secondTime=[NSDate dateWithNaturalLanguageString:@"23 April 2013"];


BOOL first=([nowTime compare:firstTime] == NSOrderedDescending);//now time is more then firsttime
BOOL second=([nowTime compare:secondTime] == NSOrderedAscending);//now time is more then secondtime
if(first && second){
    ;//return YES;
    NSLog(@"Between");
}
else{
    ;//return NO;
    NSLog(@"Not in Between");
}

EDIT:

I checked it with time as:

NSDate *firstTime=[NSDate dateWithNaturalLanguageString:@"5PM"];
NSDate *secondTime=[NSDate dateWithNaturalLanguageString:@"10PM"];

And it is working good.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Check this out

  NSString *openTime = @"10:00 AM";
    NSString *closeTime = @"10:00 PM";
    NSString *str = @"11:09 PM";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mma"];

    NSDate *firstTime =  [[NSDate alloc]init];
    firstTime = [dateFormatter dateFromString:openTime];

    NSDate* secondTime =  [[NSDate alloc]init];
    secondTime = [dateFormatter dateFromString:closeTime];
    NSDate* nowTime =  [[NSDate alloc]init];
    nowTime = [dateFormatter dateFromString:str];

    NSTimeInterval interval =[[NSDate date] timeIntervalSince1970];
    NSLog(@"interval : %f",interval);

    if([nowTime timeIntervalSince1970] >= [firstTime timeIntervalSince1970] && [nowTime timeIntervalSince1970] <= [secondTime timeIntervalSince1970])
    {

       return YES;// Between Interval

    }
    else
   {
   return NO; // Not between Interval
    }

Hope it helps you.

Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61
0

Convert your dates to timeInterval, like:

NSDate* firstTime = (NSDate *)openTime;//10:00AM
NSDate* secondTime = (NSDate *)closeTime;//10:00PM
NSDate* nowTime = (NSDate *)str;//3:09PM
NSTimeInterval firstTimeInterVal = [firstTime NSTimeIntervalSince1970]
NSTimeInterval secondTimeInterVal = [secondTime NSTimeIntervalSince1970]
NSTimeInterval nowTimeInterval = [nowTime NSTimeIntervalSince1970];

Then compare like:

if (nowTimeInterval > firstTimeInterVal && nowTimeInterval < secondTimeInterVal) {
    /**Between**/
}else{
    /**Not Between**/
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109