1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Quick iPhone SDK noob Questions.

Discussion in 'iPhone/iPad Programming' started by jmac1074, Apr 24, 2008.

  1. macrumors newbie

    I have a few questions:

    1) How can I use the UILabel class to display an Integer?

    2) How can I get the current time for use in my app?


  2. macrumors member

    Well, I just uploaded a tutorial onto http://iphonedevcentral.org/ on how to show a int with a UILabel, but it will take a few hours for it to get processed.

    As for telling the time, I believe there is a type of NS object you can use to get the time.
  3. macrumors newbie

    To display an int in a UILabel, you convert it to an NSString and set the value of the UILabel's 'text' property:

    UILabel *myLabel = [[UILabel alloc] init];
    int myInt = 1234;
    myLabel.text = [NSString stringWithFormat:@"%d", myInt];
    (Ref: https://developer.apple.com/iPhone/library/documentation/UIKit/Reference/UILabel_Class/Reference/UILabel.html)

    To get the current date/time, use the 'date' class method of NSDate:

    NSDate *now = [NSDate date];

    Edit: I just re-read your post, and it seems you're asking for the current time (not including the day/month/year?). If so, you can use an NSDateFormatter to do the job:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];
    Note: There's a few different style constants you can use (instead of NSDateFormatterShortStyle) to get different time formats, but if you want exact control over the time format, you can specify your own format strings.

    (Ref: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html)
  4. macrumors newbie

    thanks. this really helped a lot.
  5. macrumors newbie

    Also, do you know if there is any way to parse the date into integers? like one int for hours, one for minutes, and one for seconds?
  6. macrumors newbie

    Yes, here's one way (there might be a better way, but this works):

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy"];
    int year = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"MM"];
    int month = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"dd"];
    int day = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"HH"];
    int hour = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"mm"];
    int minute = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter setDateFormat:@"ss"];
    int second = [[dateFormatter stringFromDate:[NSDate date]] intValue];
    [dateFormatter release];
    Note: when getting the hour, I'm using 'HH' to specify 24-hour time; 7pm would become an int of value 19
  7. macrumors newbie

    I found another way to do this (possibly more efficient, but I'm not sure) using the NSDateComponents class:

    NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDate *date = [NSDate date];
    NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
    NSInteger year = [dateComponents year];
    NSInteger month = [dateComponents month];
    NSInteger day = [dateComponents day];
    NSInteger hour = [dateComponents hour];
    NSInteger minute = [dateComponents minute];
    NSInteger second = [dateComponents second];
    [calendar release];
  8. macrumors regular

    You can use the dayOfWeek:, dayOfMonth etc methods of NSCalendarDate (a subclass of NSDate) to easily get the values you are after.
  9. macrumors member

    Legacy API: NSCalendarDate
    This class does not exist in iPhone OS and is deprecated in Mac OS X.

Share This Page