从两个NSDates计算完整的经过时间

时间:2016-08-24 18:06:15

标签: objective-c nsdate nsdateformatter nsdatecomponents

我正在尝试创建一种方法,以下列格式计算全部时间:
0 Year 0 month 1 day 1 hour 15 minutes 5 seconds

我将以下两个日期作为例子。这应该是1小时15分5秒。

NSString * startDate = @"2016-08-24 10:00:00";
NSString * endDate   = @"2016-08-24 11:15:05";
NSLog(@"Total Time: %@", [self caluculateTimeFrom:startDate endDate:endDate]);

进行计算的方法

- (NSString*) caluculateTimeFrom:(NSString*)startDate endDate:(NSString*)endDate {

    NSLog(@"Start Date: %@", startDate);
    NSLog(@"End Date: %@", endDate);
    if (startDate == nil || endDate == nil) {
        return nil;
    }


    NSString * returnDate = @"";

    NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init];
    [dateFormater setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

    NSDate * date1 = [dateFormater dateFromString:startDate];
    NSDate * date2 = [dateFormater dateFromString:endDate];

    float totalElapsedSeconds = roundf([date2 timeIntervalSinceDate:date1]);
    NSLog(@"totalElapsedTime: %f", totalElapsedSeconds);

    if (totalElapsedSeconds < SECOND ) {
        returnDate = @"0 seconds";
        return returnDate;
    }

    float days = totalElapsedSeconds / (60 * 60 * 24);
    totalElapsedSeconds -= days * (60 * 60 * 24);
    float hours = totalElapsedSeconds / (60 * 60);
    totalElapsedSeconds -= hours * (60 * 60);
    float minutes = totalElapsedSeconds / 60;

    returnDate = [NSString stringWithFormat:@"%f days %f hours %f minutes", days, hours, minutes];


    return returnDate;
}

我已经看过了SO的静止,但他们似乎只吐了一个实例。即1天前,但没有剩余的完整字符串。

我做错了什么? 请不要使用外部Github框架或CocoaPods。因为大多数people recommend没有达到我想要的目标。

更新用户Rob建议使用 NSDateComponentsFormatter

要获得我想要的样式,以便将来使用。这仅适用于iOS 8 +

NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
    formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
    formatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

1 个答案:

答案 0 :(得分:0)

我认为你的计算方法实现应该是这样的:

- (NSString *)caluculateTimeFrom:(NSString*)startDate endDate:(NSString*)endDate
{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];

    NSDate *date1 = [formatter dateFromString:startDate];
    NSDate *date2 = [formatter dateFromString:endDate];

    NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];

    NSCalendarUnit units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:units
                                               fromDate:[NSDate date]];
    [components setYear:1];
    [components setMonth:1];
    [components setDay:1];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    NSDate *zeroDate = [calendar dateFromComponents:components];
    NSDate *date = [NSDate dateWithTimeInterval:interval sinceDate:zeroDate];

    components = [calendar components:units
                             fromDate:date];
    NSString *result = [NSString stringWithFormat:@"%ld Year %ld month %ld day %ld hour %ld minutes %ld seconds", components.year - 1,
                        components.month - 1,
                        components.day - 1,
                        components.hour,
                        components.minute,
                        components.second];

    return result;
}