NSDateFormatter dateFromString [英] NSDateFormatter dateFromString

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

问题描述

来自我的数据源的字符串的格式如下:

The string which comes from my data source is formatted like this:

2011-04-11 23:12:05

2011-04-11 23:12:05

// dateString comes from my data source, and look like I've said like this.
dateString = @"2011-04-11 23:12:05";

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMMM"];

NSDate *date = [formatter dateFromString:dateString];

当我在此类线程之后搜索Internet时,答案始终是dateString的格式错误.但是我的dateString是正确的字符串之一,对吧?

When I've searched internet after threads like these the answer has always been that the dateString had wrong format. But my dateString is one of the correct ones, right?

为什么日期返回nil?

Why does date returns nil?

推荐答案

日期格式字符串@"MMMM"表示您要解析的字符串是一个月的全名(一月",十月"等).

A date format string of @"MMMM" means that the string you're trying to parse is the full name of a month ("January", "October", etc).

您想要更多类似的东西:

You want something more like:

yyyy-MM-dd HH:mm:ss

查看日期格式设置模式以获取更多信息.

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

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