Quick iPhone SDK noob Questions.

Discussion in 'iOS Programming' started by jmac1074, Apr 24, 2008.

  1. jmac1074 macrumors newbie

    Apr 24, 2008
    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. MDMstudios macrumors member

    Mar 18, 2008
    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. mysticwhiskey macrumors newbie

    Mar 31, 2008
    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. jmac1074 thread starter macrumors newbie

    Apr 24, 2008
  5. jmac1074 thread starter macrumors newbie

    Apr 24, 2008
    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. mysticwhiskey macrumors newbie

    Mar 31, 2008
    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. mysticwhiskey macrumors newbie

    Mar 31, 2008
    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. stadidas macrumors regular

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

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

Share This Page