如何在iphone中将字符串转换为NSDate? [英] How to convert string to NSDate in iphone?
问题描述
我现在正在服务器下面的日期,我想将字符串转换为日期。
I am geting below date from the server now I want to convert string to date.
2011年6月8日星期三晚上11:58美国东部时间
Wed, 08 Jun 2011 11:58 pm EDT
但我不能成功请给我建议。
谢谢
But I cant success please give me advice. Thanks
推荐答案
操作方法:将字符串转换为NSDate
In Objective-C ,您通常需要将字符串转换为 NSDate
对象。一种简单的方法是使用NSDateFormatter对象。它提供了dateFromString方法,该方法将字符串转换为NSDate对象。但是,您需要告诉 NSDateFormatter
日期的格式。请参阅下面的示例:
How-To: Convert a string to NSDate
In Objective-C, you commonly nead to convert a string to an NSDate
object. A simple way to do this is using the NSDateFormatter object. It provides the dateFromString method which converts a string into an NSDate object. You do, however, need to tell NSDateFormatter
the format the date will be in. See below example:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss a"];
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
阅读 EDT - 东部夏令时 我认为将EDT更改为-0004会更好。
And read about EDT – Eastern Daylight Time I think it would be better you change EDT to -0004.
这篇关于如何在iphone中将字符串转换为NSDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!