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..
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..
Hi,
What you really need is to compare two objects of the same kind.
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
if current-date is a string too, make it an NSDate. if its already an NSDate, leave it.
..
NSString *date = @"2009-05-11"
NSString *nowDate = [[[NSDate date]description]substringToIndex: 10];
if([date isEqualToString: nowDate])
{
// your code
}
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
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.