Cocoa touch 重新格式化字符串

Cocoa touch 重新格式化字符串,cocoa-touch,nsstring,Cocoa Touch,Nsstring,我需要帮助。我有一个UIlabel中的日期,格式为2013年4月26日。如何将其重新格式化为2013年4月26日?我已经有以下代码: UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)]; [timelabel setText:dateToday]; dateToday值为04/26/2013 NSDateFormatter *dateFormat = [[NSDateFormatter

我需要帮助。我有一个UIlabel中的日期,格式为2013年4月26日。如何将其重新格式化为2013年4月26日?我已经有以下代码:

UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectMake(xx, xx, xx, xx)];
[timelabel setText:dateToday];
dateToday
值为
04/26/2013

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM.dd.yyyy"];
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];
NSLog(@"today : %@", stringFromDate);
增加了错误检查。始终检查输入字符串是否为有效的日期字符串

参考:

增加了错误检查。始终检查输入字符串是否为有效的日期字符串


参考资料:

Smart!如果
dateToday
包含无效值怎么办?聪明!如果
dateToday
包含无效值怎么办?
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"];
}
timelabel.text = [dateToday stringByReplacingOccurrencesOfString:@"/" withString:@"."];