Wil*_*ill 2 date objective-c nsdate nsdateformatter ios
所以我收到的日期字符串如下所示:"2013-03-20T21:13:26-7:00",我从网络后端收到.我无法控制后端,只是一个fyi.
我的偏好是将日期格式化为:3月20日晚上9:13.
当我做以下
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *date = [dateFormatter dateFromString:@"2013-03-20T21:13:26-7:00"];
Run Code Online (Sandbox Code Playgroud)
date 一片空白.
我的第一个想法是日期字符串看起来很奇怪,也许我应该删除T和"-7:00",因为"-7:00"会附加到我收到的每个日期,我不知道是什么那是为了.
即使在字符串看起来像@"2013-03-20 21:13:26"之后,date仍然是空的.
我承认我不是格式化日期的专家,所以如果我能在这个问题上得到一些帮助,那就太好了.
提前致谢!
Anu*_*das 14
您必须将dateFormat设置为dateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dateFormatter dateFromString:@"2013-03-20T21:13:26-7:00"];
[dateFormatter setDateFormat:@"hh:mma 'at' MM/yy"];
NSString *dateString = [dateFormatter stringFromDate:date];
Run Code Online (Sandbox Code Playgroud)