xcode 我想从字符串转换为日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7803713/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:10:19  来源:igfitidea点击:

I want convert from string to date

xcodestringdate

提问by ???

NSDate *LastBuildDate;    
NSDate *PubDate;

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMddhhmmss"];
LastBuildDate = [formatter dateFromString:buttoninfoamtion.mLastdate];
PubDate=[formatter dateFromString:newsItem.mPubDate];

   if([PubDate compare:LastBuildDate])

buttoninfomation.mlastdate and newsItem.mPubDate is string from OpenAPI

buttoninfomation.mlastdate 和 newsItem.mPubDate 是来自 OpenAPI 的字符串

ex) buttoninfomation.mlastdate: Tue, 18 Oct 2011 15:54:43 +0900
newspub:Tue, 18 Oct 2011 15:58:00 +0900

例如)
buttoninfomation.mlastdate:2011 年 10 月 18 日星期二15:54:43 +0900 新闻发布:2011 年 10 月 18 日星期二 15:58:00 +0900

but
LastBuidDate = (null)
PubDate = (null)


LastBuidDate = (null)
PubDate = (null)

I want to compare two factor .

我想比较两个因素。

So date- date or string- string but I think string - string is so complex.

所以日期-日期或字符串-字符串,但我认为字符串-字符串太复杂了。

what wrong up codes?

什么错误的代码?

采纳答案by Jon Skeet

Well the format you've given of "yyyyMMddhhmmss" doesn't match "Tue, 18 Oct 2011 15:54:43 +0900" even slightly.

好吧,您给出的“yyyyMMddhhmmss”格式与“Tue, 18 Oct 2011 15:54:43 +0900”甚至略微不匹配。

Try

尝试

EEE, dd MMM yyyy HH:mm:ss Z

as the format string - see the iOS documentationand the Unix TR#35for more information.

作为格式字符串 -有关更多信息,请参阅iOS 文档和 Unix TR#35