Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

ios - Why my NSDateFormatter returns 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];

My strDate returns

21/12/2012

but when:

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

My strDate returns null

This is very weird, anyone has experienced the same?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use HH instead of hh.

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...