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.
Try this:
if ([someDate timeIntervalSinceNow] < 0.0) {
// Date has passed
}
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]])
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With