views:

2966

answers:

4

how to compare two date in objective c

that is . i have two date..

2009-05-11 and current-date

i want check the given date is current-date or not...

how it is possible...

pls help me detailed ..

thanks and regards .. by raju..

+1  A: 

Hi,

What you really need is to compare two objects of the same kind.

  1. create an NSDate out of your String date (@"2009-05-11") :
    http://blog.evandavey.com/2008/12/how-to-convert-a-string-to-nsdate.html

  2. if current-date is a string too, make it an NSDate. if its already an NSDate, leave it.

yn2
You can't compare two objects, by using ==
oxigen
my mistake, answered another question at the same time. Thank you for commenting that.
yn2
+1  A: 

..

NSString *date = @"2009-05-11"
NSString *nowDate = [[[NSDate date]description]substringToIndex: 10];
if([date isEqualToString: nowDate])
{
// your code
}
oxigen
I think it'd be much more useful to turn "date" into an NSDate object and then compare NSDate objects. This code will work, but substringToIndex:10 is really a hack. If you need more information, such as which date is more recent - you'd need to do it that way, too.
Ben Gotow
Of course, you can compare NSDate objects.But NSDate has information about Date and TIMEYour need 1. Create NSDate from string 2. REmove from current-date information about time (set time to 00:00:00)And only after that compare dates. I think that this way is not easy and quick.
oxigen
If you're only interested in the day itself, you can create NSDates then use NSCalendar to get NSDateComponents out of it, at which point you can numerically compare just the values you're interested in. You could also get components for the number of days since the epoch, to use in a single comparison.
Jim Dovey
A: 

Hi,

Here buddy. This function will match your date with any specified date and will tell weather they match or not. You can also modify the components to match your requirements.

- (BOOL)isSameDay:(NSDate*)date1 otherDay:(NSDate*)date2 {
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];}

Regards, Naveed Butt

A: 

NSDate *today = [NSDate date]; // it will give you current date NSDate *newDate = [NSDate dateWithString:@"xxxxxx"]; // your date

NScomparionResult result; //has three possible values: NSOrderedSame,NSOrderedDescending, NSOrderedAscending

result = [todat compare:newDate]; // comparing two dates

if(result==NSOrderedAscending) NSLog(@"today is less"); else if(result==NSOrderedDescending) NSLog(@"newDate is greater"); else NSLog(@"Both dates are same");

There are other ways that you may use to compare an NSDate objects. Each of the methods will be more efficient at certain tasks. I have chosen the compare method because it will handle most of your basic date comparison needs.

Sajjad