I have a date coming from the database which in this format 2011-11-30 18:38:00 +0000 and i need date difference from now.How it’s is possible?this what i am doing:-
NSDate* pDateEnd;
NSLog(@"PDateEnd=%@",pDateEnd); //it has value=2011-11-30 18:38:00 +0000
NSTimeInterval secForDeal = [pDateEnd timeIntervalSinceNow];
NSLog(@"secForDeal=%i",secForDeal);
if(secForDeal < 0)
return nil;
//////
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents* pRemainingDate = [[[NSDateComponents alloc] init] autorelease];
[pRemainingDate setHour:0];
[pRemainingDate setMinute:0];
[pRemainingDate setSecond:secForDeal];
//preparing date
NSDate *date = [currentCalendar dateFromComponents:pRemainingDate];
//again splitting date to components
NSDateComponents *comps = [currentCalendar components:unitFlags fromDate:date];
/////Generating the date
NSString* pRetTimeRemaining = [NSString stringWithFormat:@"%i Year %i Month %i days %i Hr %i Min",[comps year],[comps month],[comps day],[comps hour],[comps minute]];
///////
NSLog(@"pRetTimeRemaining=%@",pRetTimeRemaining);
return pRetTimeRemaining;
Please tell if there is any error or another way to do this.Thank you.
Try following code for get number of days, min, hrs, months between two dates.
Try following code for get number of days between two dates.
Or fololw
How can i get the Date Difference?
How to convert an NSTimeInterval (seconds) into minutes