Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDate, comparing two dates [duplicate]

Ok, I have been working on this for a couple of days now and am stuck. What I am trying to do is compare the current time with a predetermined time. What I want to happen is, Everyday at certain times I want certain code to fire, but not before the time of day that I specify.

so, basically if the "current time" is the same or equal to "predetermined time" then fire this code. Or fire this line of code.

Sounds simple, but I am having trouble grabbing comparing the two times.

I have formatted a string to a date like so

     NSString* dateString = @"11:05 PM";
        NSDateFormatter* firstDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
        [firstDateFormatter setDateFormat:@"h:mm a"];
        [firstDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
        NSDate* date = [firstDateFormatter dateFromString:dateString];
        NSLog(@"date %@",date);

then i grab the current time like so

   NSDate *currentTime = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone]     secondsFromGMT]];
NSDateComponents* comps = [[NSCalendar currentCalendar] 
                           components: NSHourCalendarUnit |NSMinuteCalendarUnit 
                                |NSSecondCalendarUnit fromDate:currentTime];
 [[NSCalendar currentCalendar] dateFromComponents:comps];

SO how do I compare these two times? I have tried everything I can think of, please help.

like image 625
Mike Owens Avatar asked Mar 20 '12 04:03

Mike Owens


People also ask

How do you compare two dates equal?

There are two ways to check if two dates are equal in Java : Date's equals() method - return true if two dates are equal. Date's compareTo() method - return zero if two dates are equal.

How do you compare two dates in a type script?

Call the getTime() method on each date to get a timestamp. Compare the timestamp of the dates. If a date's timestamp is greater than another's, then that date comes after.

How do you compare two date objects in JavaScript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

How can I compare two dates in C?

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).


1 Answers

Can you try below lines of code?

switch ([dateOne compare:dateTwo]){
     case NSOrderedAscending:
          NSLog(@"NSOrderedAscending");
    break;
    case NSOrderedSame:
          NSLog(@"NSOrderedSame");
    break;
    case NSOrderedDescending:
         NSLog(@"NSOrderedDescending");
    break;
}

But in your case I think you need to keep both dates in same format ..or some thing like this

 NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    [components setHour:23];//change your time to 24hrs formate 
    [components setMinute:05];
    [components setSecond:00];


    NSDate *dateOne = [[NSCalendar currentCalendar] dateFromComponents:components];

    components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];

     NSDate *dateTwo = [[NSCalendar currentCalendar] dateFromComponents:components];

and do comparison between dateOne and dateTwo.

like image 93
Raj Avatar answered Nov 10 '22 16:11

Raj