TheLearner
TheLearner

Reputation: 19507

How do I determine whether an NSDate (including time) has passed

How do I determine whether an NSDate (including time) has passed? I need to basically compare today's date to a date in the db but am stumped.

Upvotes: 23

Views: 12633

Answers (3)

Nik Burns
Nik Burns

Reputation: 3423

you need to use an NSDate comparison, many answers on here will assist you.

iOS: Compare two dates

logic will need tweaking, but this should set you in the right direction:

- (BOOL)date:(NSDate*)date isBefore:(BOOL)before otherDate:(NSDate*)otherDate ;
{
    if(before && ([date compare:otherDate] == NSOrderedAscending))
        return YES;
    if (!before && ([date compare:otherDate] == NSOrderedDescending))
        return YES;  
}

usage:

if([self date:yourDate isBefore:YES otherDate:[NSDate date]]) 

Upvotes: 6

Simon Withington
Simon Withington

Reputation: 1485

You can use

[NSDate date];

to get an NSDate object representing the current time and date.

Then compare that to the date you are analysing, for example:

if ([currentDate timeIntervalSince1970] > [yourDate timeIntervalSince1970]) {
// yourDate is in the past
}

you can use this to compare any two dates. Hope this helps.

Upvotes: 2

mattjgalloway
mattjgalloway

Reputation: 34912

Try this:

if ([someDate timeIntervalSinceNow] < 0.0) {
    // Date has passed
}

Upvotes: 92

Related Questions