我需要帮助。我在此标签的UIlabel中有此日期04/26/2013。我怎样才能将其格式化为04.26.2013?我已经有以下代码:
UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)];
[timelabel setText:dateToday];
dateToday
值为04/26/2013
。
答案 0 :(得分:2)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM.dd.yyyy"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSLog(@"today : %@", stringFromDate);
答案 1 :(得分:1)
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"];
}
添加了错误检查。始终检查输入字符串是否是有效的日期字符串。
参考:Date Formatting Guide (though it is for Mac; iOS applies the same)
答案 2 :(得分:1)
timelabel.text = [dateToday stringByReplacingOccurrencesOfString:@"/" withString:@"."];