在我的iPhone应用程序中将字符串转换为日期 [英] Convert string to date in my iPhone app

查看:95
本文介绍了在我的iPhone应用程序中将字符串转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为iPhone制作了日历应用程序,其中我的日期为字符串格式(例如Tue,2010年5月25日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

我需要使用什么?

推荐答案

查看 NSDateFormatter的类引用。您使用它像这样:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆