Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a method for generating a readable elapsed time string between two dates. #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NSDate+Utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 44 additions & 0 deletions NSDate+Utilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,50 @@ + (NSDate *) dateWithMinutesBeforeNow: (NSInteger) dMinutes
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
return newDate;
}
#pragma mark - Elapsed 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
Expand Down