Create a date in xcode label
13,507
Solution 1
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
Solution 2
Creating the text date is simple, but the real question is what is your data source that you want to make the date text out of?
Solution 3
The date can be formatted and set in label as below.
NSDate *today = [NSDate date];
NSDateFormatter *dformat = [[NSDateFormatter alloc] init];
[dformat setDateFormat:@"dd:MM:YYYY"];
myLabel.text = [dformat stringFromDate:today];
Author by
shy
Updated on June 04, 2022Comments
-
shy almost 2 years
First of all I want to say thanks in advance for helping me.
I want to know how can I create a date on a label using Xcode, and the date will follow the same date like in iphone.
Thanks
-
Matthias Bauch almost 13 yearsreplace
[formatter setDateFormat:@"MMM dd, YYYY"]
with[formatter setDateStyle:NSDateFormatterLongStyle];
to prevent a 2 star app store rating from me. Believe it or not, there are countries that use a different dateFormat than yours. When you usedateFormat
to present dates to the user you are doing it wrong. -
shy almost 13 yearssorry i try to use this code but the date is not coming out on my apps.?? any idea..
-
johnoodles almost 13 yearswell, that is why I termed it simple approach... Anyway,I changed it to a less obvious one. Thanks @fluchtpunkt for the comment though.