我可以使用NSDateFormatter将此日期字符串转换为NSDate吗? [英] Can I use NSDateFormatter to convert this date string to an NSDate?

查看:110
本文介绍了我可以使用NSDateFormatter将此日期字符串转换为NSDate吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串......

I have this string...

2010-08-24T16:00:00-05:00

我想从中提取时间部分(即16:00)并将其转换为12 - 小时(即04:00 pm)。我正在尝试使用NSDateFormatter来完成此任务,但它无法正常工作...

and I'd like to extract the time portion from it (i.e. 16:00) and convert it to its 12-hour equivalent (i.e. 04:00 pm). I'm trying to use NSDateFormatter to accomplish this, but it's not working...

NSDateFormatter* dateformatter = [[NSDateFormatter alloc] init];
[dateformatter setDateFormat:@"hh:mm a"];
NSDate *date1 = [dateformatter dateFromString:[listOfTimes objectAtIndex:0]];
[dateformatter release];

我可以将NSDateFormatter与此日期格式一起使用吗?如果没有,我如何提取时间并将其转换为相当于12小时的时间?

Can I use NSDateFormatter with this date format? If not, how can I extract the time and convert it to its 12-hour time equivalent?

谢谢!

推荐答案

问题与解析冒号有关。我问了同样的问题,解决方案在这里:如何在iOS中将日期字符串解析为NSDate对象?

The problem has to do with parsing the colon. I asked the same question and the solution is here: How to parse a date string into an NSDate object in iOS?

这篇关于我可以使用NSDateFormatter将此日期字符串转换为NSDate吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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