Benches Uncategorized

4

NSTimeInterval to NSString formatted as N…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi,

I have two NSDates and want to display the time interval between these both dates as a string formatted in hours and minutes (H:mm). I was searching for it a long time, but didn't find a way to go through for this specific case.

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    K T

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Anything here?

    http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFor matting/Articles/dfDateFormatting104.html#//appleref/doc/uid/TP40002369-SW1

    Might want to convert to seconds, calculate the diff and then convert back...

  2. 0

    Tobe

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I already have the time interval as NSTimeInternal. The real problem is how format the time interval like a date into a string.

  3. 0

    etresoft

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Tobe wrote:

    I already have the time interval as NSTimeInternal. The real problem is how format the time interval like a date into a string.

    You can't. A timer interval is just a double. If you want to represent it as a formatted time string, you'll have to do it yourself. The following is standard procedure:


    - (NSString *) formatInterval: (NSTimeInterval) interval
    {
    unsigned long seconds = interval;
    unsigned long minutes = seconds / 60;
    seconds %= 60;
    unsigned long hours = minutes / 60;
    minutes %= 60;
    NSMutableString * result = [[NSMutableString new] autorelease];
    if(hours)
    [result appendFormat: @"%d:", hours];
    [result appendFormat: @"%02:", minutes];
    [result appendFormat: @"%02", seconds];
    return result;
    }

    Enhance and/or fix bugs as needed.

  4. 0

    Tobe

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    horrible, but works: thanks a lot!