NSDateFormatter 不起作用 [英] NSDateFormatter not working

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

问题描述

好吧,我问这个问题有点愚蠢,但知道为什么这不起作用:

Ok I feel kind of dumb asking this, but any idea why this is not working:

 {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"mmmm dd, yyyy"];

        NSDate *myDateFromString = [formatter dateFromString:creditDate.text];
         NSLog(@"%@", myDateFromString);
        [self.credit setCertificationDate: myDateFromString];
    }

返回空

推荐答案

格式和字符串不匹配,因为m"是分钟而M"是月.因此无法解释2012 年 8 月 18 日",请改用MMMM dd yyyy"您可以在 unicode.org

Format and string don't match because "m" is minute and "M" is month. So "August 18 2012" can't be interpreted, take "MMMM dd yyyy" instead You can check formats at unicode.org

我总是明确设置语言环境:

I always set a locale explicitly:

  NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
  [dateFormatter setLocale:usLocale];
  [usLocale release];

只要您不解析月份名称,如果提供显式格式字符串,您的真实语言环境是否为 en_US 并不重要.

As long as you don't parse month names, it doesn't matter if your real locale is en_US if supplying an explicit format string.

更多提示:

  • 使用硬编码字符串进行测试以排除提供的日期错误.
  • 尝试更简单的格式化字符串,例如不将时间或月份作为数字来隔离错误部分.
  • 如果还是不行,你可以使用 NSDateFormatter 的 getObjectValue:forString:range:error: 来获取更详细的错误信息.

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

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