How do I compare in objective C to see if a certain time and date period overlaps another that is already in a plist for example?
This is most commonly used in booki
Try this to Compare ..
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss aa"];
NSDate *dateOne=[dateFormat dateFromString:@"01/09/2011 11:12:10 AM" ];
NSDate *dateTwo = [NSDate date];
switch ([dateOne compare:dateTwo]){
case NSOrderedAscending:
//NSLog(@"NSOrderedAscending");
break;
case NSOrderedSame:
//NSLog(@"NSOrderedSame");
{
break;
case NSOrderedDescending:
//NSLog(@"NSOrderedDescending");
break;
}
[dateFormat release];
Assuming you have the date stored as a NSDate, just use this:
[myDate timeIntervalSinceNow];
That returns a NSTimeInterval which is just a float representing the time interval in seconds. If it is negative that means that the time that you are comparing is previous.
If your date is stored in the plist as a NSString
you have to convert it to a NSDate
first, use a NSDateFormatter
for that.
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //Change this to your date format
NSDate * date = [formatter dateFromString:yourString];
[formatter release];