-3

HI i have two date pickers.

datepicker1, datepicker2.

I need datepicker1.date to compare with datepicker2.date

i need to verify that the two dates are same or not . How is it possible. Thanks in advance.i have used the below one but it is not working

if([datePicker.date isEqualToDate:FromPicker.date])
Melbourne
  • 531
  • 3
  • 24
  • You can look at the answer [here](http://stackoverflow.com/a/7736757/443292). I would recommend you research before you post such questions. – Shri Jun 25 '12 at 09:14

4 Answers4

1

Use the standard comparison method, compare:

André Morujão
  • 6,963
  • 6
  • 31
  • 41
1

Use the -compare: method of NSDate:

if([date1 compare:date2] == NSOrderedSame)
{
    //They are the sme    
}
graver
  • 15,183
  • 4
  • 46
  • 62
1

I think you want to ignore time component. Do it by below way:

 NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
 NSDateComponents *components1 = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                       fromDate:YOUR_FIRST_DATE];
 NSDateComponents *components2 = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                        fromDate:YOUR_SECOND_DATE];

 if ([components1 year] == [components2 year] &&
     [components1 month] == [components2 month] &&
     [components1 day] == [components2 day]) 
 {
           //dates are same...
 }
 else
 {
           //dates are different...
 }
Apurv
  • 17,116
  • 8
  • 51
  • 67
0

i would suggest using timeIntervalSince1970

NSDate *date1; // your first date
NSDate *date2; // your second date

if ([date1 timeIntervalSince1970] == [date2 timeIntervalSince1970]) {
    //Equal
}
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56