开发者

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开发者_JAVA百科 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.

// 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

  • How can i get the Date Difference?

  • How to convert an NSTimeInterval (seconds) into minutes


You can get hour from below code.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜