0

I want to check if user selected date is the same as the current date or not. I am using this code. I can check if the selected date is in the past or future, but I can't detect if it is the same, because when I log both dates it is in this format. I mean there is difference between today date time and the selected date time. So it is considered as less. Here is log:

selected date:2014-01-03 05:53:53 +0000 , today date: 2014-01-03 05:54:20 +0000

Here is my code:

 NSDate *today = [NSDate date]; // it will give you current date
NSDate *newDate = datePicker.date; // your date

NSComparisonResult result;
//  has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending
NSLog(@"selected date:%@ , today date: %@",newDate,today);
result = [today compare:newDate];
NSLog(@"result %d",result);
if(result==NSOrderedAscending)
{
    NSLog(@"big ascending");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;

}
else if(result==NSOrderedDescending){
    NSLog(@"fine");
    [UIView animateWithDuration:0.25 delay:0.0 options: UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         // self.view.frame=CGRectMake(0, -30, 320, 200);
                         subview.frame=CGRectMake(0, 640, 320, 180);
                         //                             CGRect f1;
                         //                             f1=self.view.frame;
                         //                             f1.origin.y=f1.origin.y-100;
                         // self.view.frame=f1;
                         // v1.backgroundColor=[UIColor blackColor];
                     }
                     completion:^(BOOL finished){
                         if(finished)  {NSLog(@"Finished end !!!!!");}
                     }];
}

else{
    NSLog(@"same");
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:@"Birthdate should be less than today date."
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return;
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
vivek bhoraniya
  • 1,526
  • 2
  • 17
  • 36
  • Please check it out! It may help you. http://stackoverflow.com/questions/8183472/how-to-compare-two-nsdate-objects-in-objective-c – Gaurav Jan 03 '14 at 06:03

4 Answers4

1

Try this-

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"MM-dd-yyyy"];
NSDate *date1 = [dateFormat dateFromString:firstDate];
NSDate *date2 = [dateFormat dateFromString:secondDate];

NSComparisonResult comparisonResult = [date1 compare:date2];

if(comparisonResult==NSOrderedAscending)
{
}
else if(comparisonResult ==NSOrderedDescending)
{
}
else
{
    // dates are same
}
Rahul Mane
  • 1,005
  • 18
  • 33
1
- (BOOL)isSameDay:(NSDate *)date1 withDate2:(NSDate *)date2
{
    if (nil == date1 || nil == date2) {
        return NO;
    }

    NSCalendar *calendar = [NSCalendar currentCalendar];

    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents* comp1 = [calendar components:unitFlags fromDate:date1];
    NSDateComponents* comp2 = [calendar components:unitFlags fromDate:date2];

    return [comp1 day] == [comp2 day] &&
    [comp1 month] == [comp2 month] &&
    [comp1 year]  == [comp2 year];
}

use this method to compare if the two date is the same day.

johnMa
  • 3,291
  • 24
  • 37
0

You'll need to use the below method to convert sec. into millisecond:

  long time1=[[NSDate date] timeIntervalSince1970];
  long time2=[newDate timeIntervalSince1970];

After that you can compare as normal long variable.This is good practice.

You can also use

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

Formate you date with NSDateFormatter and remove the "time" from date. here is the date class:

     NSDateFormatter *format = [[NSDateFormatter alloc] init];
     [format setDateFormat:@"MM/dd/yyyy"];
Imran Ahmed
  • 1,023
  • 1
  • 11
  • 23