Question

I've got several NSTimeIntervals derived from previous calculations on various NSDates. Now I want to display these intervals to the user in days:hours:minutes:seconds format.

Earlier in my app, I use this code to display the information in a slightly different context:

-(void)updateDurationLabel
{
    //Which calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //Gets the componentized interval from the most recent time an activity was tapped until now

    NSDateComponents *components= [calendar components:NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:self.startTime toDate:[NSDate date] options:0];
    NSInteger days = [components day];
    NSInteger hours = [components hour];
    NSInteger minutes = [components minute];
    NSInteger seconds =[components second];

    // Converts the components to a string and displays it in the duration label, updated via the timer

    cellLabelTempText = [NSString stringWithFormat:@"%02i:%02i:%02i:%02i",days,hours,minutes,seconds];
    self.durationOrSleepLabel.text = cellLabelTempText;
}

However, in the current case, I would like for the desired NSString to be produced using various already-obtained NSTimeIntervals for use in several different labels, which are created on the fly.

Question:

Is there some way to get directly to an NSString from an existing NSTimeInterval, i.e., without going through the fromDate/toDate rigamarole?

Thanks!

Update:

Pursuant to @Rich's answer below, I added this method, which skirted the math:

-(void) focusItemDurationCalculator
{
    NSInteger days = ((NSInteger) focusItemDuration) / (60 * 60 * 24);
    NSInteger hours = (((NSInteger) focusItemDuration) / (60 * 60)) - (days * 24);
    NSInteger minutes = (((NSInteger) focusItemDuration) / 60) - (days * 24 * 60) - (hours * 60);
    NSInteger seconds = ((NSInteger) round(focusItemDuration)) % 60;

    actualDurationFocusItem = [NSString stringWithFormat:@"%02i:%02i:%02i:%02i",days,hours,minutes,seconds];
}

Works great!

Was it helpful?

Solution

Why not just use the NSTimeInterval and calculate each component yourself. You can also do away with using [NSDate date], and use timeIntervalSinceNow.
You can use integer division to discard the fractional part of each component.

// Use abs if you don't care about direction
NSTimeInterval duration = abs([self.startTime timeIntervalSinceNow]);

NSInteger days = ((NSInteger) duration) / (60 * 60 * 24);
NSInteger hours = (((NSInteger) duration) / (60 * 60)) - (days * 24);
NSInteger minutes = (((NSInteger) duration) / 60) - (days * 24 * 60) - (hours * 60);
NSInteger seconds = ((NSInteger) round(duration)) % 60;

This should be quicker than dealing with NSDateComponents. It's not exactly what you're looking for but it does cover this :D

without going through the fromDate/toDate rigamarole

OTHER TIPS

Unfortunately there is no way to skip the calculation step. I would recommend writing you a category on NSDate or NSString and implemented the desired conversion there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top