0

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.

Community
  • 1
  • 1
Coldsteel48
  • 3,482
  • 4
  • 26
  • 43

2 Answers2

4

In general, I'd prefer to use NSDateComponents and NSCalendar for these sorts of calendrical calculations, since you don't know what calendar the user is using.

Here's a method for doing the comparison in a category on NSDate using date components:

#import <Foundation/Foundation.h>

@interface NSDate (Foo)
- (BOOL)isAfterFourPM;
@end

@implementation NSDate (Foo)

- (BOOL)isAfterFourPM {
    unsigned int flags = NSHourCalendarUnit;
    NSDateComponents *comps = [[NSCalendar currentCalendar] components:flags fromDate:self];
    return (comps.hour >= 16);
}

@end

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSDate *now = [NSDate date];
        NSLog(@"is after 4 PM? - %@",([now isAfterFourPM]?@"YES":@"NO"));

        //  let's try with a different time (17h)
        unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
        flags |= NSHourCalendarUnit;
        NSDateComponents *currentComps = [[NSCalendar currentCalendar] components:flags fromDate:now];
        currentComps.hour = 17;
        NSDate *afterFourDate = [[NSCalendar currentCalendar] dateFromComponents:currentComps];

        NSLog(@"is after 4 PM? - %@",([afterFourDate isAfterFourPM]?@"YES":@"NO"));
    }
}

This prints:

2013-12-10 05:16:05.921 Untitled 2[43453:507] is after 4 PM? - NO
2013-12-10 05:16:05.921 Untitled 2[43453:507] is after 4 PM? - YES

(at ~ 5 AM Central time)

In this case, all you need to do, is get the NSHourCalendarUnit component from the date using the current calendar and compare the hour property on NSDateComponents to 16.

FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • I don't get it *_* , the code you posted is not good ? or is it good ? – Coldsteel48 Dec 10 '13 at 11:49
  • Works for me. The point I was making is that you don't need to craft a "4PM today" date solely for the purposes of making the "after 4PM" comparison. You can make the comparison using the date components API. See the method `isAfter4PM` above. – FluffulousChimp Dec 10 '13 at 11:52
  • Oh , well I used the answer from above - "NSInteger hour = [[df stringFromDate:now] integerValue];" so now I have an integer that holds the hour , and then I compared it to another integer that was set to 16 , I don't know in current time it works now , but do you think it may have problems in other time formats? – Coldsteel48 Dec 10 '13 at 11:52
  • It's a simple enough comparison (just the hour), that I think it's fine as you posted in your edits. – FluffulousChimp Dec 10 '13 at 12:08
2

The following will give you hour:

NSDate *now = [NSDate date];

NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"hh"];

NSInteger hour = [[df stringFromDate:now] integerValue];

NOTE:

If you want to check only for 4 then use hh, if for 16 then use HH.

Or in simple words HH for 24 hour format.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • and then i should compare between 2 integers ? i will give it a try thanks! – Coldsteel48 Dec 10 '13 at 11:00
  • Worked like very well !! Thank you very much ! (I was sure there is something simpler than 4 pages of code) :) – Coldsteel48 Dec 10 '13 at 11:11
  • This leaves open the question (posed in the code comments) about how you get "today at 4 PM". For that you'll need to deal with `NSDateComponents` on `[NSDate date]` – FluffulousChimp Dec 10 '13 at 11:26
  • @Eric: It depends on what is your requirement. If user need to compare only for current date, then this will work. Thats is what I solved. Otherways using NSDateComponent this can be done... – Anoop Vaidya Dec 10 '13 at 11:33