Find Exact difference between two dates

Posted by iPhone Fun on Stack Overflow See other posts from Stack Overflow or by iPhone Fun
Published on 2010-12-28T07:33:10Z Indexed on 2010/12/30 3:54 UTC
Read the original article Hit count: 216

Hi all ,

I want some changes in the date comparison.

In my application I am comparing two dates and getting difference as number of Days, but if there is only one day difference the system shows me 0 as a difference of days.

I do use following code

NSDateFormatter *date_formater=[[NSDateFormatter alloc]init];

[date_formater setDateFormat:@"MMM dd,YYYY"];

NSString *now=[NSString stringWithFormat:@"%@",[date_formater stringFromDate:[NSDate date]]];

LblTodayDate.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",now]];

NSDate *dateofevent = [[NSUserDefaults standardUserDefaults] valueForKey:@"CeremonyDate_"];

NSDate *endDate =dateofevent;  

NSDate *startDate = [NSDate date];

gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned int unitFlags = NSDayCalendarUnit;

NSDateComponents *components = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];

int days = [components day];

I found some solutions that If we make the time as 00:00:00 for comparision then it will show me proper answer , I am right or wrong i don't know.

Please help me to solve the issue

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk-3.0