ios IOS如何设置日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18098647/
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
IOS how to set date format
提问by billwang1990
I have a string like this
我有一个这样的字符串
NSString *datestr = @"2013-08-06T03:51:54+00:00";
I try to set dateformat as below:
我尝试将日期格式设置如下:
NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
[dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dformat dateFromString:datestr];
But the date will be nil, so, can anyone tell me how to set the dateformat, thx!
但是日期将为零,所以,谁能告诉我如何设置日期格式,谢谢!
回答by Abdullah Shafique
Try this:
尝试这个:
NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
[dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
NSDate *date = [dformat dateFromString:datestr];
Add ZZZ
to the end of your formatter.
添加ZZZ
到格式化程序的末尾。
回答by user1548843
Try out this code
试试这个代码
NSString *datestr = @"2013-08-06T03:51:54+00:00";
NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
[dformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssz"];
NSDate *date = [dformat dateFromString:datestr];
use small zwhere z give you - with the specific non-location format. Where that is unavailable, falls back to localized GMT format. Use one to three letters for the short format or four for the full format. In the short format, metazone names are not used unless the commonlyUsed flag is on in the locale
在 z 给你的地方使用小 z- 使用特定的非位置格式。如果不可用,则回退到本地化的 GMT 格式。短格式使用一到三个字母,完整格式使用四个字母。在短格式中,除非在 locale 中打开了 commonUsed 标志,否则不使用 metazone 名称
for zone for more info about Date Format Patterns in objective c refer this link http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patternshope this will help you
有关目标 c 中日期格式模式的更多信息,请参阅此链接 http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns希望这对您有所帮助
回答by Tarek Hallak
try below:
试试下面:
NSString *dateString = @"2013-08-06T03:51:54+00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:dateString range:nil error:&error];