ios yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 格式的 NSString 到 NSDate
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20993049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format
提问by kagmanoj
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?
我得到的日期字符串是这种格式的 2014-01-08T21:21:22.737+05:30。我如何将它授予 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?
它返回零。有什么想法吗?
回答by Javid
Use this code it is working fine your dateFormatter
was wrong .
使用此代码它工作正常你dateFormatter
错了。
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!!!!!!!!!!.
快乐编码!!!!!!!!!!!!
回答by rckoenes
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
.
您的日期格式包含一个时区作为文字:yyyy-MM-dd'T'HH:mm:ss.SSS+05:30
在+05:30
它的时区定义。你应该用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);
回答by 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];
回答by Hussain Shabbir
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
在代码中像下面这样初始化。因为如果不总是初始化,您将获得空值:-
dateFormatter=[[NSDateFormatter alloc]init];
回答by Sahil Mahajan
Set your dateFormat as
将您的 dateFormat 设置为
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
And Yes, Now NSLog is Printing a specific Date.!!!
是的,现在 NSLog 正在打印一个特定的日期。!!!
Enjoy Coding
享受编码