将NSString转换为NSDate [英] Convert NSString to NSDate

查看:102
本文介绍了将NSString转换为NSDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
将NSString转换为NSDate(并再次转换)

Possible Duplicate:
Converting NSString to NSDate (and back again)

我向flickr api发出请求,它以以下格式向我返回了日期

I make a request to the flickr api and it returns me the date in the following format

"2013-02-01T06:25:47Z"

如何将其转换为NSDate格式?

How do i convert this into NSDate format??

推荐答案

这是一个简单的方法,我们使用dateFromString方法使用NSDateformatter将NSString转换为NSDate.我们需要为NSString提供现有的Dateformatter样式

It's a simple one, converting NSString to NSDate we use NSDateformatter using dateFromString method. We need to provide the Dateformatter style with existing style for NSString

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [dateFormatter dateFromString:@"2013-02-01T06:25:47Z"];
NSTimeZone *pdt = [NSTimeZone timeZoneWithAbbreviation:@"PDT"];
[dateFormatter setTimeZone:pdt];
[dateFormatter setDateFormat:@"HH:mm:ss zzz"];
[dateFormatter setDateFormat:@"K:mm a, z"];
NSString * updated String = [dateFormatter stringFromDate:date];

这篇关于将NSString转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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