在我的 iPhone 应用程序中将字符串转换为日期 [英] Convert string to date in my iPhone app
本文介绍了在我的 iPhone 应用程序中将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我为 iPhone 制作了一个日历应用程序,其中我有一个字符串格式的日期(例如Tue, 25 May 2010 12:53:58 +0000").
I have made a calendar application for the iPhone in which I have a date in string format (e.g. "Tue, 25 May 2010 12:53:58 +0000").
我想将其转换为 NSDate
.
我需要用什么来做到这一点?
What do I need to use to do that?
推荐答案
看看 classNSDateFormatter 的参考.你像这样使用它:
Take a look at the class reference for NSDateFormatter. You use it like this:
NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
有关如何自定义 NSDateFormatter 的更多信息,请尝试 本参考指南.
For more information on how to customize that NSDateFormatter, try this reference guide.
请注意,这将解析完整的月份名称.如果您需要三个字母的月份名称,请使用 LLL
而不是 LLLL
.
Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL
instead of LLLL
.
这篇关于在我的 iPhone 应用程序中将字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文