Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jmac1074

macrumors newbie
Original poster
Apr 24, 2008
6
0
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?

Thanks.

Jmac
 
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.
 
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?

Thanks.

Jmac

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

Code:
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:

Code:
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:

Code:
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)
 
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?
 
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?

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

Code:
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
 
I found another way to do this (possibly more efficient, but I'm not sure) using the NSDateComponents class:

Code:
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];
 
You can use the dayOfWeek:, dayOfMonth etc methods of NSCalendarDate (a subclass of NSDate) to easily get the values you are after.

Legacy API: NSCalendarDate
This class does not exist in iPhone OS and is deprecated in Mac OS X.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.