Get system time
Sometimes you might need to show the system time on your iPhone or iPad app. There are many ways to accomplish this task and I would like to discuss one of the ways we are using here at Walden Systems, because we believe this is the most straightforward method to show system time.
Strategy to show system time is straight forward: First, we need to declare a variable of type NSDate to get the time. Second, we need to declare a variable of type NSDateFormatter to set the formatting. Third, we need to declare a variable of type NSString to show to the screen. Fourth, we need to get the system time. Fifth, we need to format the time and set it to a variable.
Here are actual code samples:
The first thing you need to do is declare a variable to get the time and a variable to format the time. To do so, use the following line:
1 NSDate * time ; 2 NSDateFormatter * format ;
Then you need to declare a variable to hold the formatted string for the time and then get the system time.
1 NSString * timeStr ; 2 time = [ NSDate date ] ;
Finally, you set the formatted time to a string
1 format = [ [ NSDateFormatter alloc ] init ] ; 2 [ format setDateFormat : @"HH:mm:ss" ] ; 3 timeStr = [ format stringFromDate : l_time ] ;
So, this is how a final code for resizing your picture will look like:
1 NSDate * time ; 2 NSDateFormatter * format ; 3 NSString * timeStr ; 4 5 time = [ NSDate date ] ; 6 format = [ [ NSDateFormatter alloc ] init ] ; 7 [ format setDateFormat : @"HH:mm:ss" ] ; 8 timeStr = [ format stringFromDate : time ] ;