Search code examples
objective-cios6nsdate

iOS 6: Conversion from NSString to NSDate not working


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"];

Solution

  • 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