现在可以使用此代码:
NSString *myDate = @"06/18/2015 8:26:17 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
NSDate *date = [dateFormatter dateFromString:myDate];
[dateFormatter setDateFormat:@"dd.MM. HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.timeLabel.text = dateString;
答案 0 :(得分:2)
您必须将日期格式设置为字符串
NSString *myDate = @"06/18/2015 8:26:17 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"];
NSDate *date = [dateFormatter dateFromString:myDate];
//Set New Date Format as you want
[dateFormatter setDateFormat:@"dd.MM. HH:mm"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
NSLog(@"%@",[dateFormatter stringFromDate:date]);
答案 1 :(得分:0)
我认为您的代码Dohi7没有任何问题,因为我测试了它并且使用@“dd.MM.HH:mm”和@“dd.MM.H:mm”两种格式正确显示日期。请通过在控制台中打印来再次检查dateFromString的值。
是的Ashish也是对的。您不需要分配数据对象,您可以直接使用dateFromString函数,该函数本身返回NSDate对象。
答案 2 :(得分:0)
谢谢,现在它可以使用以下代码:
NSString *myDate = @"06/18/2015 8:26:17 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
NSDate *date = [dateFormatter dateFromString:myDate];
[dateFormatter setDateFormat:@"dd.MM. HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:date];
cell.timeLabel.text = dateString;