计算小时差iOS

时间:2016-03-09 09:21:39

标签: ios nsdate

我这样写是为了计算小时差。

+ (NSInteger)hoursBetweenDate:(NSDate*)fromDateTime andDate:(NSDate *)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    [calendar rangeOfUnit:NSHourCalendarUnit startDate:&fromDate
             interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSHourCalendarUnit startDate:&toDate
             interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSHourCalendarUnit
                                           fromDate:fromDate toDate:toDate options:0];

    return [difference hour];
}

问题是如果输入是这样的话,

  
    

2016-03-09 07:58:39 +0000(fromDateTime)     2016-03-09 09:07:44 +0000(toDateTime),

  

差异总是2小时。实际上,它应该是1小时10分钟。结果,我只想显示1小时。是否有办法向上或向下舍入?

4 个答案:

答案 0 :(得分:2)

如果您只对日期之间的“无格式”小时数感兴趣,可以使用NSTimeInterval并轻松计算。

+(int)fullHoursBetweenDate:(NSDate *)date1 andDate:(NSDate *)date2 {
    NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
    return (int)interval / 3600;
}

答案 1 :(得分:0)

这个小的NSCalendar扩展应该为你做一个技巧。

extension NSCalendar {
    func hoursBetweenDates(startDate: NSDate, endDate: NSDate) -> Int {
        let days  = differenceInDates(startDate, endDate: endDate, unit: .Hour)
        return days
    }

    func differenceInDates(startDate: NSDate, endDate: NSDate, unit: NSCalendarUnit) -> Int {
        let startUnit    = ordinalityOfUnit(unit, inUnit: .Era, forDate:startDate)
        let endUnit      = ordinalityOfUnit(unit, inUnit: .Era, forDate:endDate)
        let difference   = endUnit-startUnit

        return difference
    }
}

Obj-C版本可以写成如下:

@interface NSCalendar (Duration)
-(NSNumber *)hoursBetweenStartDate:(NSDate*)startDate andEndDate:(NSDate*) endDate;
@end

@implementation NSCalendar (Duration)
-(NSNumber *)hoursBetweenStartDate:(NSDate*)startDate andEndDate:(NSDate*) endDate {

    NSUInteger startUnit = [self ordinalityOfUnit:NSCalendarUnitHour inUnit:NSCalendarUnitEra forDate:startDate];
    NSUInteger endUnit   = [self ordinalityOfUnit:NSCalendarUnitHour inUnit:NSCalendarUnitEra forDate:endDate];

    return [NSNumber numberWithUnsignedInteger: (endUnit - startUnit)];
}
@end

答案 2 :(得分:0)

在比较之前,您只提取了小时组件(NSHourCalendarUnit),这就是为什么它总是2( 9 - 7 )。添加了NSMinuteCalendarUnit,结果将是1:

+ (NSInteger)hoursBetweenDate:(NSDate*)fromDateTime andDate:(NSDate *)toDateTime
{
    NSDate *fromDate;
    NSDate *toDate;

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

    [calendar rangeOfUnit:NSHourCalendarUnit|NSMinuteCalendarUnit startDate:&fromDate
                 interval:NULL forDate:fromDateTime];
    [calendar rangeOfUnit:NSHourCalendarUnit|NSMinuteCalendarUnit startDate:&toDate
                 interval:NULL forDate:toDateTime];

    NSDateComponents *difference = [calendar components:NSHourCalendarUnit
                                               fromDate:fromDate toDate:toDate options:0];

    return [difference hour];
}

此外,如果您的目标是iOS 8+,则应使用NSCalendarUnitHourNSCalendarUnitMinute而不是NSHourCalendarUnit,并且已弃用NSMinuteCalendarUnit

答案 3 :(得分:0)

根据您的要求修改此功能

+(NSString*)timeDifference:(NSDate*)startDate endDate:(NSDate*)endDate{

NSDateComponents *components;
NSInteger days;
NSInteger hour;
NSInteger minutes;
NSString *strDuration;


components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute
                                             fromDate: startDate toDate: endDate options: 0];
days = [components day];
hour=[components hour];
minutes=[components minute];

if(days>0){

    if(days>1){
        strDuration=[NSString stringWithFormat:@"%d days",days];
    }
    else{
        strDuration=[NSString stringWithFormat:@"%d day",days];

    }
    return strDuration;
}

if(hour>0){

    if(hour>1){
        strDuration=[NSString stringWithFormat:@"%d hours",hour];
    }
    else{
        strDuration=[NSString stringWithFormat:@"%d hour",hour];

    }
    return strDuration;
}

if(minutes>0){

    if(minutes>1){
        strDuration=[NSString stringWithFormat:@"%d minutes",minutes];
    }
    else{
        strDuration=[NSString stringWithFormat:@"%d minute",minutes];

    }
    return strDuration;
}

return @"";

}