NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

kagmanoj picture kagmanoj · Jan 8, 2014 · Viewed 26.2k times · Source

My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate?

I tried:

       NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
       [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"];
       NSDate *currentDate = [dateFormatter dateFromString:currentDateString];

       NSLog(@"CurrentDate:%@", currentDate);

It returns nil. Any thoughts?

Answer

Javid picture Javid · Jan 8, 2014

Use this code it is working fine your dateFormatter was wrong .

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];

NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];        
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];

NSLog(@"CurrentDate:%@", currentDate);

Happy coding!!!!!!!!!!.