2016-08-24 75 views
0

計算經過充分友好的時候我試圖創建一個計算的格式全部時間的方法:
0 Year 0 month 1 day 1 hour 15 minutes 5 seconds從兩個NSDates

我傳遞了以下兩個日期的例子。應該是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 quiestions但theyu一切似乎只吐出了1個實例。即1天前,但不是剩餘的完整字符串。

我在做什麼錯? 請不要使用外部的Github框架或CocoaPods。作爲最people recommend不完成我想要的。

更新用戶使用羅布NSDateComponentsFormatter

爲了得到我想要這裏的風格是未來使用的代碼提示。在iOS 8+這隻能

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

一旦你有你的兩個'NSDate'對象,使用'NSDateComponentsFormatter'建立您的字符串的時間已過去。 – Rob

+0

這不起作用。它吐出'剩下大約75分鐘' –

+1

@Firemarble你需要正確配置NSDateComponentsFormatter來得到你想要的。 – rmaddy

回答

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; 
} 
+0

您應該解釋什麼是錯的,以及您的解決方案如何解決問題。僅有代碼的答案在這裏不受歡迎。 – rmaddy