I really need help. The code result is 2:0:0 while the format is set to hh:mm:ss. I want the result to be 2:00:00 (adding 0 in front of the minutes and seconds when they are under 10).
NSDateFormatter *test = [[NSDateFormatter alloc] init];
[test setDateFormat:@"HH:mm:ss"];
NSDate *date1 = [test dateFromString:@"18:00:00"];
NSDate *date2 = [test dateFromString:@"20:00:00"];
NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned int uintFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* differenceComponents = [gregorian components:uintFlags fromDate:date1 toDate:date2 options:0];
NSLog(@"%d:%d:%d",[differenceComponents hour],[differenceComponents minute],[differenceComponents second]);
How to do this?
Log using the
%02ldspecifier, for example:Output:
Additionally create
NSStringslike this: