0

我有一个这种格式的字符串:

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];

谁能告诉我这里有什么问题?
格式模板是否正确?

4

2 回答 2

-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];
于 2013-08-08T04:20:34.777 回答
-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);
于 2013-08-08T05:00:28.033 回答