1

Here is the code that I have for my two datepickers which I need to compare them to the current date with an if condition or a switch.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);


NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);

in my .h

@property (weak, nonatomic) IBOutlet UIDatePicker *startTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *endTime;

How do I do that? Do I need to store them to an NSDate or NSTime since I only really need the time?

Also, is there an If range in Objective C?

Thanks,

user1949873
  • 460
  • 7
  • 17

4 Answers4

2

The only dates I see in your code are startTime.date and endTime.date. I don't know where you are getting those, but they are presumably NSDates. The current date, on the other hand, is [NSDate date]. Hence you can compare using NSDate's compare: method or any of several other comparison methods (see the NSDate documentation).

matt
  • 515,959
  • 87
  • 875
  • 1,141
2

NSDate implements isEqualToDate: which tests if two dates are the same down to the microsecond. It also implements earlierDate: and laterDate: which you can use to build a range check.

You could also build some helpers that let you control the sensitivity of equality check using timeIntervalSinceDate:. There's no date range object, but you could add a between check to your helpers, too...

@interfce NSDate (Comparison)

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance;
- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance;

@end

@implementation NSDate (Comparison)

- (BOOL)isEqualToDate:(NSDate *)aDate within:(NSTimeInterval)tolerance {

    NSTimeInterval difference = [self timeIntervalSinceDate:aDate];
    return fabs(difference) < tolerance;
}

- (BOOL)isBetween:(NSDate *)start and:(NSDate *)end within:(NSTimeInterval)tolerance {

    NSTimeInterval startDifference = [self timeIntervalSinceDate:start];
    if (startDifference < tolerance) return NO;

    NSTimeInterval endDifference = [end timeIntervalSinceDate:self];
    if (endDifference < tolerance) return NO;

    return YES;
}

@end

I haven't tested these.

danh
  • 62,181
  • 10
  • 95
  • 136
  • NSDate has comparison methods so why reinvent that wheel? (Not criticism, I'm thinking I might be missing something) – matt Apr 06 '13 at 03:04
  • It's a valid question. I added test for inclusion in a range, which I think the OP needs, and the idea of reducing the sensitivity of the test. – danh Apr 06 '13 at 04:44
2

Just add this:

if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime"); 
}

if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime"); 
}

If you want to calculate interval between two dates use this method

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


if([startTime.date timeIntervalSinceDate:[NSDate date]] > 0)
{
  //start time greater than today
}

else if([startTime.date timeIntervalSinceDate:[NSDate date]] < 0)
{
    //start time less than today
}

else
{
   //both dates are equal
}

For knowing when the application has entered background use notifications add this statement in your viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnteredBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];

Add function to cater the notification when it is posted

-(void)applicationEnteredBackground
{
   //do all the above steps here when you want.
}

And in dealloc function of the class remove observer for notification

-(void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • Excellent straight answer! do you know how can I link it to the `AppDelegate`'s `DidEnterBackground` method? – user1949873 Apr 06 '13 at 04:16
  • @user1949873 means what u want there?,do you want to use any value? – iSpark Apr 06 '13 at 10:19
  • 1
    Okay let me get this straight, you want to do all this when the application enters background state? OK see my edit then. – Satheesh Apr 06 '13 at 12:16
  • 1
    Are you okay with the above implementation?, because I am not doing in appdelegate'd DidEnterBackground function insted I am doing it in other way but more or less the same. – Satheesh Apr 06 '13 at 12:23
2

you can also do like below by using NSDate's compare: method,

 NSDateFormatter* df = [[NSDateFormatter alloc] init];
 [df setDateFormat:@"MM-dd-yyyy"];
 NSDate* date1 = [df dateFromString:@"12-23-2046"];

 NSDate *date2 = [NSDate date];

 if ([date1 compare:date2] == NSOrderedSame){
    NSLog(@"Same");
 }
 if ([date2 compare:date1]==NSOrderedAscending ) {
    NSLog(@"AScending");
 }
 if ([date2 compare:date1]== NSOrderedDescending) {
    NSLog(@"Descending");

 }

take your startTime,endTime instead of date1,hope it will helps you.

iSpark
  • 952
  • 7
  • 18