Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 948
TheLearner Avatar asked Jan 05 '12 16:01

TheLearner


3 Answers

Try this:

if ([someDate timeIntervalSinceNow] < 0.0) {
    // Date has passed
}
like image 159
mattjgalloway Avatar answered Nov 20 '22 09:11

mattjgalloway


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]]) 
like image 42
Nik Burns Avatar answered Nov 20 '22 08:11

Nik Burns


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.

like image 2
Simon Withington Avatar answered Nov 20 '22 09:11

Simon Withington