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

孤街浪徒 提交于 2019-11-30 17:42:50

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

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);
Lait kumar
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];

I think you need to initialize the dateFormatter like this below in your code. Because if do not initialze always you will get the null value:-

dateFormatter=[[NSDateFormatter alloc]init];

Set your dateFormat as

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

And Yes, Now NSLog is Printing a specific Date.!!!

Enjoy Coding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!