从NSDateFormatter获取空 [英] Getting Null from NSDateFormatter

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

问题描述

我正在尝试将String转换为NSDate,但是我得到了Null.

I am trying to convert a String into NSDate, but I am getting Null.

示例字符串输入为= 8/8/2013 12:32:22 PM

Sample String Input is = 8/8/2013 12:32:22 PM

代码:

[self.dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSLog(@"Date %@", [[communications objectAtIndex:x] objectForKey:@"dateUpdated"]);
NSDate *date = [[NSDate alloc] init];
date = [self.dateFormat dateFromString:[[communications objectAtIndex:x] objectForKey:@"dateUpdated"]];
NSLog(@"Date from NSDate %@", [self.dateFormat stringFromDate:date]);

我在做什么错了?

示例NSLog输出

2013-08-15 13:01:59.409 HD[478:907] Date 8/8/2013 12:32:22 PM
2013-08-15 13:01:59.412 HD[478:907] Date from NSDate (null)  
2013-08-15 13:01:59.418 HD[478:907] Date 8/12/2013 9:45:18 AM
2013-08-15 13:01:59.419 HD[478:907] Date from NSDate (null)
2013-08-15 13:01:59.421 HD[478:907] Date 8/12/2013 9:45:20 AM
2013-08-15 13:01:59.423 HD[478:907] Date from NSDate (null)
2013-08-15 13:01:59.430 HD[478:907] Date 8/12/2013 4:10:49 PM
2013-08-15 13:01:59.431 HD[478:907] Date from NSDate (null)

注意:真正奇怪的是,如果我在Simulator上运行相同的代码,它在大多数设备上都可以正常工作.出于某种原因,如果我在一些最新的设备上运行它,它将返回null ...

Note: What's really strange is that if I run the same code on Simulator it works fine and most devices. For some reason, if I run it on a handful of devices that are all up to date, it returns null...

推荐答案

它可能与语言环境有关,但与

It is probably locale-dependent, but with

[self.dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

它应可在所有设备上使用.

it should work on all devices.

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

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