Search code examples
iphoneobjective-cios

NSDateComponents formatting issues


I'm using several NSDateComponents to display a clock whose individual values are broken up throughout the screen but I'm having formatting troubles.

The seconds display 1-60 not 00-59, and the hours are in 24hr format not 12. Is there any way to format these differently?

Right now I've got the values coming through as an NSInteger, transfering them to a NSString and applying the string to a UILabel. Can anyone help?

is there a @"%" format i should be using? (right now i'm using @"d") And if so where could I find a list of those different types?


Solution

  • As you already have the values as NSInteger then assuming they are called minutes and hours first:

    minutes -= 1; // convert 1 -> 60 to 0 -> 59
    if(hours > 12) hours -= 12; // convert 13 -> 24 to 1 -> 12
    

    Now to format them both use "%02d" - broken down that is 0 - zero padding, 2 - field width, d - decimal integer.

    You can find all the format specifications (apart from %@ for objects) by looking up the documentation on printf