1

I know this probelm has been discussed many times on SO, but none of the results solve my problem. Here is the code snippet,

NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSZ"];         
NSDate* date = [df dateFromString:dateTaken]; 
/* dateTaken gets its value
 from the server as 2006-01-23T20:50:27Z */

I hav tried adding breakpoints and checked, dateFromString is returning nil. Once I get a date Object, I would change its format and display a different string but I am not getting a date Object itself.

What could be the issue here ?

2
  • Shouldn't the last SSZ be SS'Z'?
    – user529758
    Aug 2, 2012 at 12:34
  • 1
    Talkallikar please accept and upvote my answer, if helped. Thank you.
    – user529758
    Aug 2, 2012 at 12:39

3 Answers 3

1

The last part of the format string should be

SS'Z'

instead of

SSZ
1

Seems that your formatter is wrong. It should be:
yyyy-MM-dd'T'HH:mm:ss'Z'
2006-01-23T20:50:27Z

Match it up to the string you have and there is no SSZ component.

EDIT:
After reviewing the other answer, I got nervous that mine was wrong, so I tried the following:
NSString* dateTaken = @"2006-01-23T20:50:27Z";
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SS'Z'"];
NSDate* date = [df dateFromString:dateTaken]; //nil
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
date = [df dateFromString:dateTaken]; // Not nil

Seems that with the string you provided, you need the 2nd format I just provided.

3
  • yeah. but the answer H2CO3 gave, was already working for me. :) Aug 2, 2012 at 13:11
  • If it works, then great. You should confirm what actual output you are getting in the dateTaken string. Because if it exactly matches what you posted, then if you run the code I put above with the .SS'Z' it shouldn't be working. I don't need to get the check, just wanted to make sure I wasn't giving the wrong answer.
    – Liron
    Aug 2, 2012 at 13:38
  • Ya. sure! actually I was resolving bugs in someone else's code :P Aug 3, 2012 at 5:16
0

The retrieved format is wrong verify the format to convert

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.