-3

I wanted to convet the string to date.

Date in string format is: 2016-09-12T09:52:39Z (Without any space)

Most of the solutions which I found has atleast space in between date and time text. Which is not working in my case.

The major issue in converting above date format is that "T" and "Z". I think some how the date formatter is not distingushing date "dd" and timezone "T". I did attempted to format that but its not working.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];

NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:datestring];

Any solution to convert such string into date?

Edit: I had mention while posting question is that - In most of question related to "converting string to date" has spaces in bettwen those text, So, You can write formatter according to that. In my case, There was no space between date and hour part & instead it has T. Hence, I was not able to convert the string date into date object, Instead I was getting null. For which I tried some solutions & after that I posted the question.

JiteshW
  • 2,195
  • 4
  • 32
  • 61

1 Answers1

0

enter image description here

plz check your datestring. it's working fine in my end.

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
  • This answer is incorrect. Do NOT quote the `Z` in the date format. The `Z` in the date string represents the date's timezone. `Z` mean UTC timezone. If you quote the `Z` in the date format, the `Z` is ignored and the date string is parsed as if it is in local time instead of UTC time and you get the wrong date. – rmaddy Sep 12 '16 at 14:30
  • What comment? Look at the image in your answer. The value of `date` shows a time of 4:22:39 UTC but the string being parsed is 9:52:39 UTC. They should be the same but they are not due to the mistake of quoting the Z in the format string. – rmaddy Sep 13 '16 at 03:56
  • jitesh comment i do nothing with his code . i only show him that you code is working plz chek datestring variable ok's. read the previous comment . – balkaran singh Sep 13 '16 at 03:59
  • @rmaddy, Ok, I got your point regarding incorrect result if we keep Z in quote. But it was not formatting date if I put Z without quotes. Hence I posted the question. – JiteshW Sep 13 '16 at 05:46