shy shy - 4 months ago 9
Objective-C Question

create a date in xcode label

HI all,

first of all want to say thanks in advance for helping me...

i want to know how can i create a date in label using xcode.
and the date will follow the same date like in iphone...

thanks

im still new and got lots to learn..

Answer

Here's one simple approach

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//uncomment to get the time only
//[formatter setDateFormat:@"hh:mm a"];
//[formatter setDateFormat:@"MMM dd, YYYY"];
[formatter setDateStyle:NSDateFormatterMediumStyle];


//get the date today
NSString *dateToday = [formatter stringFromDate:[NSDate date]];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320.0f, 20.0f)];
[label setText:dateToday];
[formatter release];

//then add to a view