From 8f81c5a6995cb216f397a972425ddc13ce627a45 Mon Sep 17 00:00:00 2001 From: Jeff Kunzelman Date: Thu, 27 Oct 2016 09:46:08 -0700 Subject: [PATCH 1/3] Added elapsed time method --- NSDate+Utilities.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NSDate+Utilities.h b/NSDate+Utilities.h index 53148b0..c547db4 100644 --- a/NSDate+Utilities.h +++ b/NSDate+Utilities.h @@ -25,6 +25,9 @@ + (NSDate *) dateWithMinutesFromNow: (NSInteger) dMinutes; + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes; +//elapsed time in readable string format i.e. 3h 45m ++(NSString *)getTimeElapsedStringBetween:(NSDate *)startDate endDate:(NSDate *)endDate; + // Short string utilities - (NSString *) stringWithDateStyle: (NSDateFormatterStyle) dateStyle timeStyle: (NSDateFormatterStyle) timeStyle; - (NSString *) stringWithFormat: (NSString *) format; From 077f41b586a3151d20693cd051c07a0ac36be947 Mon Sep 17 00:00:00 2001 From: Jeff Kunzelman Date: Thu, 27 Oct 2016 09:47:51 -0700 Subject: [PATCH 2/3] added elapsed time method --- NSDate+Utilities.m | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/NSDate+Utilities.m b/NSDate+Utilities.m index 3200071..70d9c19 100644 --- a/NSDate+Utilities.m +++ b/NSDate+Utilities.m @@ -80,6 +80,51 @@ + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval]; return newDate; } +#pragma mark - Elapsed Time +#pragma mark - elasped time ++(NSString *)getTimeElapsedStringBetween:(NSDate *)startDate endDate:(NSDate *)endDate { + + NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; + + NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute; + + NSDateComponents *components = [gregorian components:unitFlags + fromDate:startDate + toDate:endDate options:0]; + NSInteger years = [components year]; + NSInteger months = [components month]; + NSInteger days = [components day]; + NSInteger hours = [components hour]; + NSInteger minutes = [components minute]; + + NSMutableString *elapsedString = [NSMutableString string]; + + if (years > 0) [elapsedString appendFormat:@"%lu Y",(long)years]; + if (years > 1) [elapsedString appendFormat:@" "]; + if (years > 0) [elapsedString appendFormat:@" "]; + + if (months > 0) [elapsedString appendFormat:@"%lu M",(long)months]; + if (months > 1) [elapsedString appendFormat:@" "]; + if (months > 0) [elapsedString appendFormat:@" "]; + + if (days > 0) [elapsedString appendFormat:@"%lu D",(long)days]; + if (days > 1) [elapsedString appendFormat:@" "]; + if (days > 0) [elapsedString appendFormat:@" "]; + if (months < 1) { + if (hours > 0) [elapsedString appendFormat:@"%lu h",(long)hours]; + if (hours > 1) [elapsedString appendFormat:@" "]; + if (hours > 0) [elapsedString appendFormat:@" "]; + if (days < 1) { + if (minutes > 0) [elapsedString appendFormat:@"%lu min ",(long)minutes]; + if (minutes > 1) [elapsedString appendFormat:@" "]; + if (minutes > 0) [elapsedString appendFormat:@" "]; + } + } + + return [NSString stringWithString:elapsedString]; +} + + #pragma mark - String Properties - (NSString *) stringWithFormat: (NSString *) format From 4bad4752edfb70f0800fe2140d57216b9a9f5173 Mon Sep 17 00:00:00 2001 From: Jeff Kunzelman Date: Thu, 27 Oct 2016 09:49:27 -0700 Subject: [PATCH 3/3] Update NSDate+Utilities.m --- NSDate+Utilities.m | 1 - 1 file changed, 1 deletion(-) diff --git a/NSDate+Utilities.m b/NSDate+Utilities.m index 70d9c19..20c3cdb 100644 --- a/NSDate+Utilities.m +++ b/NSDate+Utilities.m @@ -81,7 +81,6 @@ + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes return newDate; } #pragma mark - Elapsed Time -#pragma mark - elasped time +(NSString *)getTimeElapsedStringBetween:(NSDate *)startDate endDate:(NSDate *)endDate { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];