在目标c / iOS中将日期字符串转换为NSDate [英] convert date string to NSDate in objective c/iOS

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

问题描述

我有这个字符串----> Sun,2012年12月16日15:30:22 +0000

I have this string ----> Sun, 16 Dec 2012 15:30:22 +0000

我想将其转换为NSDate。我已经尝试了下面的代码,但它给了我

I want to convert it to NSDate. I have tried the below code but it is giving me null

NSDateFormatter *df = [[NSDateFormatter alloc] init];

[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];

NSDate *date = [[NSDate alloc] init];

date = [df dateFromString: @"Sun, 16 Dec 2012 15:30:22 +0000"];

NSLog(@"date: %@", date);

我没有得到这个问题?

任何指导plz

推荐答案

格式化程序的 dateFormat 需要匹配你提供它的字符串的格式。

The dateFormat of the formatter needs to match the format of the string you are providing it with.

因此这样的事情应该有效

Therefore something like this should work

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss ZZZ";

NSLog(@"%@", [formatter dateFromString:@"Fri, 18 Jan 2013 15:30:22 +0000"]);

要查看说明符实际含义,请查看 Unicode技术标准#35

To look up what the specifiers actually mean check Unicode Technical Standard #35

这篇关于在目标c / iOS中将日期字符串转换为NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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