Reputation: 59
i need help., I have this date in a UIlabel in this format 04/26/2013 . How can I re format it into 04.26.2013 ? I already have below codes:
UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)];
[timelabel setText:dateToday];
dateToday
value is 04/26/2013
.
Upvotes: 0
Views: 54
Reputation: 54212
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM.dd.yyyy"];
NSDate *date = [dateFormatter dateFromString:dateToday];
if(date != nil) {
NSString *formattedDateString = [dateFormatter stringFromDate:date];
[timelabel setText:formattedDateString];
} else {
[timelabel setText:@"Invalid Date"];
}
Added error checking. Always check if the input string is a valid date string or not.
Reference: Date Formatting Guide (though it is for Mac; iOS applies the same)
Upvotes: 1
Reputation: 3592
timelabel.text = [dateToday stringByReplacingOccurrencesOfString:@"/" withString:@"."];
Upvotes: 1
Reputation: 571
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM.dd.yyyy"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSLog(@"today : %@", stringFromDate);
Upvotes: 2