Convert String to date Objective-C

2020-05-26 09:55发布

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:

 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?

1条回答
放我归山
2楼-- · 2020-05-26 10:25

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];
查看更多
登录 后发表回答