xcode 如何将字符串转换为 NSDate 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10766129/
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
How to convert string into NSDate format?
提问by user1402718
i want to convert the string into nsdate format.
我想将字符串转换为 nsdate 格式。
here is my string NSString str=@"Sunday, June 6, 2010 5:00:00 AM CDT";
这是我的字符串 NSString str=@"Sunday, June 6, 2010 5:00:00 AM CDT";
my code is
我的代码是
NSDateFormatter * Dateformats= [[NSDateFormatter alloc] init];
[Dateformats setDateFormat:@"MM/DD/yyyy hh:mm:ss"]; //ex @"MM/DD/yyyy hh:mm:ss"
NSDate *myDate=[Dateformats dateFromString:str];
NSLog(@"%@",myDate);
but it is displays null so show me the way to display the date in MM/DD/yyyy hh:mm:ss
但它显示为空,所以请告诉我在 MM/DD/yyyy hh:mm:ss 中显示日期的方法
please friends help me ,how to do this
请朋友帮助我,如何做到这一点
回答by jemmons
You set your formatter to expecting a string like @"05/26/2012 8:34:00"
. But you give it @"Sunday, June 6, 2010 5:00:00 AM CDT"
. The two formats are nothing alike. It doesn't know how to parse it and so returns nil
.
您将格式化程序设置为期望像@"05/26/2012 8:34:00"
. 但你给它@"Sunday, June 6, 2010 5:00:00 AM CDT"
。这两种格式完全不同。它不知道如何解析它,因此返回nil
.
The format you want for the example string you give is EEEE, MMMM d, yyyy h:mm:ss a zzz
.
您提供的示例字符串的格式是EEEE, MMMM d, yyyy h:mm:ss a zzz
.
回答by Mrunal
As you have mentioned your date string: NSString str=@"Sunday, June 6, 2010 5:00:00 AM CDT";
正如您提到的日期字符串: NSString str=@"Sunday, June 6, 2010 5:00:00 AM CDT";
The date format should be like,
日期格式应该是这样的,
[dateFormatter setDateFormat:@"EEE, MMM dd, yyyy hh:mm:ss a z"];
[dateFormatter setDateFormat:@"EEE, MMM dd, yyyy hh:mm:ss a z"];
For more details please go through this one: DateFormatter Details
有关更多详细信息,请查看此:DateFormatter 详细信息
Try this out, hope it will work for you..
试试这个,希望它对你有用..
Enjoy coding :)
享受编码:)