一聚教程网:一个值得你收藏的教程网站

热门教程

iOS解析带T的时间字符串的详解

时间:2022-06-25 23:32:34 编辑:袖梨 来源:一聚教程网

现有从服务器获取到的时间字符串:

@”2016-12-05T12:21:37″

这个和一般获取到的时间字符串不同之处是:中间有T。
从这种字符串获取NSDate方法如下:
1.

NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss";//使用带有子字符串'T'的日期格式
NSDate *date = [dateFormatter dateFromString:@"2016-12-05T12:21:37"];

2.

NSMutableString *mutableStringWithT = [[NSMutableString alloc] initWithString:@"2016-12-05T12:21:37"];
[mutableStringWithT replaceCharactersInRange:[mutableStringWithT rangeOfString:@"T"] withString:@" "];//手动将带T的时间字符串替换为@" "
 
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";//不带'T'的日期格式
NSDate *date = [dateFormatter dateFromString:mutableStringWithT];

热门栏目