为什么我的NSDateFormatter返回null? [英] Why my NSDateFormatter returns null?

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

问题描述

为什么我的NSDateFormatter返回null?
这是我的代码:

Why my NSDateFormatter returns null? Here is my code:

  NSString *dateString = @"21-12-2012 11:05:15";
  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  [dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm:ss"];
  NSDate *dateFromString = [[NSDate alloc] init];
  dateFromString = [dateFormatter dateFromString:dateString];

  [dateFormatter setDateFormat:@"dd/MM/yyyy"];
  NSString *strDate = [dateFormatter stringFromDate:dateFromString];

我的 strDate 返回

21/12/2012

但是当:

NSString *dateString = @"13-12-2012 13:17:58";

我的 strDate 返回 null

这很奇怪,有人经历过同样的事情吗?

This is very weird, anyone has experienced the same?

推荐答案

使用HH代替hh。

hh通常为12小时,而HH通常为24小时。

hh usually is 12 hour time while HH is usually 24 hour time.

[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

这是一个很好的官方参考,由Apple自己链接,是< a href =http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns =nofollow>这里。另一个好桌子是Zaph提到的这里

A good official reference for this, as linked by Apple themselves, is here. Another good table is here, as mentioned by Zaph.

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

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