time comparison not working. What could be wrong?

I worked on this code to perform a pause for my notifications through sleep time, however there is something missing that I can't quite figure it out. what could it be?

My .m code:

- (IBAction)save:(id)sender {

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);



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

    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSLog(@"Sleep time silent started and notification cancelled");

}

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

    [self pickerSelectedRow];

    NSLog(@"Time to wake up and notification rescheduled");
}


}

when I click on save button I receive this out output which means it's working fine, however, when the time comes i don't get any output saying the notification has been cancelled or rescheduled, which means it's not working!!!

Output:

2013-05-03 03:04:57.481 xxx[10846:c07] Start time is 5/3/13, 3:06 AM
2013-05-03 03:04:57.482 xxx[10846:c07] End time is 5/3/13, 3:07 AM

what am I missing?

also, would this work in the background?

Answers


You need a different compare. Perhaps something like this: (All of the following should be in your .m file)

- (IBAction)save:(id)sender {

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
    dateFormatter.timeStyle=NSDateFormatterShortStyle;
    dateFormatter.dateStyle=NSDateFormatterShortStyle;
    NSString *dateTimeString=[dateFormatter stringFromDate:_startTime];
    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];
    NSLog(@"End time is %@",dateTimeString2);

    if ([self date:[NSDate date] compareMe:_startTime]) {
        NSLog(@"currentDate is equal to startTime");

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        NSLog(@"Sleep time silent started and notification cancelled");

    }

    if ([self date:[NSDate date] compareMe:_endTime]) {
        NSLog(@"currentDate is equal to endTime");

        [self pickerSelectedRow];

        NSLog(@"Time to wake up and notification rescheduled");
    }
}

-(BOOL)date:(NSDate *)date1 compareMe:(NSDate *)date2 {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *date1Componenets = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date1];
    NSDateComponents *date2Componenets = [calendar components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:date2];

    return [date1Componenets year] == [date2Componenets year];
}

If you are going to compare dates a lot, you could put the comparing function in a different file, such as an NSDate category, but that is more complicated than I think you need.


Need Your Help

Enabled Brigded Network in Vmware Server

vmware virtualization vmware-server

I have the vmware server with this error, anyone knows how to fix it?

how to determine between 2 mouse clicks of treeviews

c# events treeview

I have two treeviews and a contexmenucontainer with the adding node function