1

date is always nil, and I am not able to understand what the issue is.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:@"2013-07-16T07:40:36.939-04:00"];
Undo
  • 25,519
  • 37
  • 106
  • 129
Peyush Goel
  • 383
  • 2
  • 3
  • 13
  • That code looks good to me. How are you verifying `date == nil`? – trojanfoe Jul 16 '13 at 13:10
  • what this indicates "-04:00" ? – sagarcool89 Jul 16 '13 at 13:14
  • @sagarcool89 "Zone"; see: http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns – trojanfoe Jul 16 '13 at 13:15
  • I think you should remove ':' from '-04:00' – sagarcool89 Jul 16 '13 at 13:17
  • 1
    possible duplicate of [Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?](http://stackoverflow.com/questions/5185230/converting-an-iso-8601-timestamp-into-an-nsdate-how-does-one-deal-with-the-utc) - See joel.d's answer. – Martin R Jul 16 '13 at 13:20
  • See also this answer: http://stackoverflow.com/a/15415127/1187415 which explains the differences between iOS 5 and 6. – Martin R Jul 16 '13 at 13:23
  • @Fogmeister What's the error then? – trojanfoe Jul 16 '13 at 13:37
  • 1
    @Fogmeister Well I've had a go and didn't find anything too obvious (I suspect it might need `ZZZZZZ` in the format string). Perhaps you can find the exact answer if it's *that* obvious... – trojanfoe Jul 16 '13 at 13:39
  • @trojanfoe given that there are maybe 4 or 5 questions asking how do you convert X date format into NSDate a bit of searching will find the answer. I have needed to find this answer myself in the past. However, now I don't need to find it. I suggest the OP should probably do a search (either on Google) or SO. They both come up with SO answers fairly well. That way he'll find the solution without having to ask yet another NSString -> NSDate question. – Fogmeister Jul 16 '13 at 13:44
  • @Fogmeister That's hardly the same as your original claim that this was "exactly the same as every other string/date conversion question ever asked on SO", is it? – trojanfoe Jul 16 '13 at 13:46
  • @trojanfoe "That's hardly the same as" what I actually wrote which was "possible duplicate of every other nsstring to nsdate question...". Please don't misquote me back to myself. – Fogmeister Jul 16 '13 at 13:50
  • @Fogmeister I was paraphrasing as you deleted your comments and I couldn't find them again. I didn't take your comments out of context though did I? – trojanfoe Jul 16 '13 at 13:51
  • @trojanfoe paraphrasing with quotes is not paraphrasing. I said "possible duplicate" due to the way that SO adds comments when you vote to close as a duplicate (as the person before me had done) (twice). Nor did I ever say that it was "that obvious" which you seem to have attributed to me too. I think we're getting slightly off topic now. If you so desperately want the answer to this (you seem to be interrogating me for it) then please search SO for it. – Fogmeister Jul 16 '13 at 13:54
  • 1
    it needed ZZZZ instead of Z to get it to work – Peyush Goel Jul 16 '13 at 14:14
  • #define DEFAULT_DATE_FORMAT_TIMESTAMP @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ" – Peyush Goel Jul 16 '13 at 14:15
  • Yep, the format `-04:00` for timezone is not recognized by the standard "Z" format. The relatively new "ZZZZZ" notation is needed. Or there are several simple ways to remove the offending ":" character so that "Z" will handle it. – Hot Licks Jul 16 '13 at 15:07
  • It is being returned from the server, so I would be reluctant to change it unless there is no other work around – Peyush Goel Jul 16 '13 at 15:14
  • The ":" can be removed by processing the received string -- no need to change the sender. But the new "ZZZZZ" format is a better choice, in most cases. – Hot Licks Jul 16 '13 at 16:13

4 Answers4

3

try like this,for adding hours use - symbol and for substacting use + symbol.

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
    NSDate *date = [dateFormatter dateFromString:@"2013-07-16T07:40:36.939-04:00"];//chnage symbal here.
    NSLog(@"%@",date);

O/P:-2013-07-16 11:40:36 +0000

Balu
  • 8,470
  • 2
  • 24
  • 41
  • @Peyush Goel:once try like this it'l helps you may be. – Balu Jul 16 '13 at 13:22
  • As I mentioned.... it worked as soon as I used ZZZZ instead of Z. Only thing troubling me is the timezones it is showing either GMT+5:30 or Indian Standard Time. I want to display IST. zzz should have worked but it isnt working. – Peyush Goel Jul 16 '13 at 14:22
0

Please check this :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS-hh:mm"];
NSDate *date = [dateFormatter dateFromString:@"2013-07-16T07:40:36.939-04:00"];
NSLog(@"%@",date);

Timezone's are using below formates :

     Z  

±hh:mm or ±hhmm

    ±hh

Let me know if you face any problems..

sagarcool89
  • 1,366
  • 8
  • 16
  • date is coming from the server and can't be altered. And I need to convert that timezone to a format so I would need that as well – Peyush Goel Jul 16 '13 at 14:23
0

Since the timestamp from the server is in the format -04:00, I had t use ZZZZ

#define DEFAULT_DATE_FORMAT_TIMESTAMP @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"
Peyush Goel
  • 383
  • 2
  • 3
  • 13
-1
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSString* str = @"2012-03-01T23:08:25.000Z";   // NOTE the change is here
NSDate* date = [df dateFromString:str];
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45