ios 将字符串转换为日期 Objective-C
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26230792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:03:13 来源:igfitidea点击:
Convert String to date Objective-C
提问by Alessio Crestani
I have seen the other answer and question but i didn't find any useful answer for me.
I have a string like 2014-10-07T07:29:55.850Z
and I want to have a date to compare each other.
If I use this code:
我已经看到其他答案和问题,但我没有找到任何对我有用的答案。我有一个像这样的字符串2014-10-07T07:29:55.850Z
,我想有一个日期来相互比较。如果我使用此代码:
NSString *fullDate = payload[@"sent_ts"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
dateFormat setDateFormat:@"yyyy-MM-ddTHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:fullDate];
My date is nil. Why?
我的日期是零。为什么?
回答by Prasanth S
Try This.
尝试这个。
NSString *dateString = @"2014-10-07T07:29:55.850Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];