RAMAN RANA
RAMAN RANA

Reputation: 1795

How to get the date difference in Hours?

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.

Upvotes: 1

Views: 566

Answers (2)

Deeps
Deeps

Reputation: 4577

You can get hour from below code.

int interval = [invoiceDate timeIntervalSinceDate:date];
int hour = interval/3600;

Upvotes: 2

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

Try following code for get number of days, min, hrs, months between two dates.

// The time interval 
NSTimeInterval theTimeInterval = 326.4;

// Get the system calendar
NSCalendar *sysCalendar = [NSCalendar currentCalendar];

// Create the NSDates
NSDate *date1 = [[NSDate alloc] init];
NSDate *date2 = [[NSDate alloc] initWithTimeInterval:theTimeInterval sinceDate:date1]; 

// Get conversion to months, days, hours, minutes
unsigned int unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;

NSDateComponents *conversionInfo = [sysCalendar components:unitFlags fromDate:date1  toDate:date2  options:0];

NSLog(@"Conversion: %dmin %dhours %ddays %dmoths",[conversionInfo minute], [conversionInfo hour], [conversionInfo day], [conversionInfo month]);

[date1 release];
[date2 release];

Try following code for get number of days between two dates.

-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today {
    NSDate *startDate = lastDate;
    NSDate *endDate = today;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    int days = [components day];
    return days; // multiply by 24 for get remain hrs.
}

Or fololw

Upvotes: 2

Related Questions