ios 在目标 C 中转换日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6724180/
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
Converting a date format in objective C
提问by farajnew
How can i convert the following date "Sun Jul 17 07:48:34 +0000 2011" to the following format "2011-07-17 07:48:34"?
如何将以下日期“Sun Jul 17 07:48:34 +0000 2011”转换为以下格式“2011-07-17 07:48:34”?
I used NSDateFormatter
as shown below but it didnt work. It gives null as a result.
我使用NSDateFormatter
如下所示,但没有用。结果是 null 。
NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init];
[objDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[objDateFormatter dateFromString:sDate];
回答by Suhail Patel
You've got your Date Format wrong for the style of Date you are passing in. Here is a document explaining the different modifiers: Date Format Patterns
对于传入的日期样式,您的日期格式错误。这是解释不同修饰符的文档:日期格式模式
To parse the Date "Sun Jul 17 07:48:34 +0000 2011", you'd need a Format like so:
要解析日期“Sun Jul 17 07:48:34 +0000 2011”,您需要如下格式:
[dateFormat setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
To get it into the following format: "2011-07-17 07:48:34", here is the full code:
要将其转换为以下格式:“2011-07-17 07:48:34”,这里是完整代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSDate *date = [dateFormatter dateFromString:sDate];
// Convert to new Date Format
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:date];
回答by steipete
If sDate is your string that's in the format "Sun Jul 17 07:48:34 +0000 2011", you have to convert that into a NSDate. Then, you can use a second NSDateFormatter to convert this date into your desired format "yyyy-MM-dd HH:mm:ss".
如果 sDate 是格式为“Sun Jul 17 07:48:34 +0000 2011”的字符串,则必须将其转换为 NSDate。然后,您可以使用第二个 NSDateFormatter 将此日期转换为您想要的格式“yyyy-MM-dd HH:mm:ss”。
If you need to figure out the string needed to convert, this site has a great reference: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
如果您需要弄清楚需要转换的字符串,这个站点有一个很好的参考:http: //unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
e.g. "Sun Jul 17 07:48:34 +0000 2011" can be parsed with
例如“Sun Jul 17 07:48:34 +0000 2011”可以解析为
[dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss ZZZ yyyy"];
As a bit of advice, non-standard dates like "Jul" (should be July) can make problems here. It's faster to just convert the needed parts in plain C, with strftime(). See the reference: http://publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst263.htm
作为一点建议,像“七月”(应该是七月)这样的非标准日期可能会在这里出现问题。使用 strftime() 在普通 C 中转换所需的部分会更快。请参阅参考资料:http: //publib.boulder.ibm.com/infocenter/iadthelp/v7r0/index.jsp?topic=/com.ibm.etools.iseries.langref.doc/rzan5mst263.htm
You can then convert the unix timestamp back in a NSDate with
然后,您可以将 unix 时间戳转换回 NSDate 中
[NSDate dateWithTimeIntervalSince1970:]
Using C for date/time parsing is usually up to 10x faster. NSDateFormatter is a slow dog.
使用 C 进行日期/时间解析通常快 10 倍。NSDateFormatter 是一条慢狗。