Comparing NSDates to check if today falls in between
thanks to you guys I have successfully converted my array of strings to NSDates. Now I'm trying to check if today's date falls in between my 2 dates (namely fromDate and toDate). I've seen the following questions but failed to implement them in my code. Be great I can have any other method or help on using the solutions given by the users.
NSDate between two given NSDates
How to Check if an NSDate occurs between two other NSDates
How can I check if an NSDate falls in between two other NSDates in an NSMutableArray
This is what I currently have:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSTimeZone *tz = [NSTimeZone localTimeZone];
[format setTimeZone:tz];
NSDate *now = [[NSDate alloc] init];
NSString *todaysDate = [format stringFromDate:now];
NSLog(@"todaysDate: %@", todaysDate);
//converting string - date
int size = [appDelegate.ALLevents count];
for (NSUInteger i = 0; i < size; i++) {
Event *aEvent = [appDelegate.ALLevents objectAtIndex:i];
//Convert fromDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *fromDate = [dateFormatter dateFromString:aEvent.fromDate];
[dateFormatter setTimeZone:tz];
NSLog(@"fromDate: %@",开发者_开发问答 fromDate);
//Convert toDate
NSDate *toDate = [dateFormatter dateFromString:aEvent.toDate];
NSLog(@"toDate: %@", toDate);
[dateFormatter release];
//Compare if today falls in between
//codes here
}
Do the following,
NSTimeInterval fromTime = [fromDate timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDate timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [[NSDate date] timeIntervalSinceReferenceDate];
NSTimeInterval is basically a double type so you can compare that if currTime is greater than one and less than the other than the date falls between those two NSDates.
精彩评论