我有一个这种格式的字符串:
2012-11-08T13:50:05.284-08:00
我需要将其转换为以下格式:
November 8, 2012 1:50 PM,
我已将日期模式识别为YYYY-MM-DDThh:mm:ss.sTZD,如下所示:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-DDThh:mm:ss.sTZD"];
NSDate *date = [dateFormat dateFromString:@"2012-11-08T13:50:05.284-08:00"];
[dateFormat setDateFormat:@"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
有人可以告诉我这里有什么问题吗?
格式模板是否正确?
答案 0 :(得分:-1)
您可以尝试这样做(只删除字符串中的最后5个字符)
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:@"2012-11-08T13:50:05.284"];
[dateFormat setDateFormat:@"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
答案 1 :(得分:-1)
试试这个:当我获得有效输出时。
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.S'-'S':'S"];
NSDate *date = [dateFormat dateFromString:@"2012-11-08T13:50:05.250-8:00"];
[dateFormat setDateFormat:@"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
NSLog(@"newDateStr is %@", newDateStr);