Hello I have seen many NSDate comparisons on this site but they all seems to be very complicated , since I need only to know if the date now is past 4pm or before 4pm
maybe there is some easy way to achieve this goal ?
[link] (Check if the time and date is between a particular date and time)
but it seems very long and complicated I just need simple bool answer past 4pm or not
- (BOOL)checkTime
{
NSDate* now = [NSDate date];
NSDate *endDate;
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
endDate = [formatter dateFromString:@"2012-12-07 16:00:00"];//well here I have a problem how do I set end day to today 4pm ?
NSLog(@"%d",[now compare:endDate]);
if([now compare:endDate] < 0)
return YES;
else if([now compare:endDate] > 0)
return NO;
else
return NO;
}
EDIT: After the answers I came up with this code
- (BOOL)checkTime
{
NSDate *date = [NSDate date];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"HH"];
int intS =9;
NSInteger HourStart = intS;
NSInteger hour = [[df stringFromDate:date] integerValue];
int hourE = 16;
NSInteger HourEnd = hourE;
if((hour >HourStart) && (hour < HourEnd))
return YES;
else
return NO;
}
For now it is works fine , but I am not sure it will work on another calendars set etc.