Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 931
kagmanoj Avatar asked Jan 08 '14 10:01

kagmanoj


3 Answers

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!!!!!!!!!!.

like image 163
Javid Avatar answered Nov 07 '22 17:11

Javid


Your date format contains a time zone as a literal: yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 the +05:30 it the time zone definition. You should parse it with Z.

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);
like image 13
rckoenes Avatar answered Nov 07 '22 18:11

rckoenes


NSString *p=@"2014-01-08T21:21:22.737+05:30";


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

[dateformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];

NSDate *datefor=[dateformat dateFromString:p];

[dateformat setDateFormat:@"MM-dd-yyyy hh:mm a"];

NSString *dateStr=[dateformat stringFromDate:datefor];


NSDate *datetype=[dateformat dateFromString:dateStr];
like image 5
Lait kumar Avatar answered Nov 07 '22 18:11

Lait kumar