First of all we create an NSDateFormatter built-in in obj-c with the name date, then we apply it by [[NSDateFormatter alloc] init]; . After that we say to the code procesor that we want our date to have HOUR/MINUTE/SECOND.
Finally we should make our date to be an string to work with alert or set value of a label , to do this we should create an string with NSString method then we use this : [date stringFromDate:[NSDate date]]
Either use
NSDateFormatter
as Carl said, or just use good oldstrftime
, which is also perfectly valid Objective-C:Maybe this will be more readable :
First of all we create an NSDateFormatter built-in in obj-c with the name date, then we apply it by [[NSDateFormatter alloc] init]; . After that we say to the code procesor that we want our date to have HOUR/MINUTE/SECOND. Finally we should make our date to be an string to work with alert or set value of a label , to do this we should create an string with NSString method then we use this : [date stringFromDate:[NSDate date]]
Have Fun with It .
You want a date formatter. Here's an example:
Here is a simple solution:
Change the
dateStyle
andtimeStyle
to match your formatting requirement.