这个非常简单的任务显然很难......
NSDate *date;
date = [someMethod here];
//I've checked with debugger that the method returns an object of type NSDate.
//The description of date at this point is: "2012-02-02 19:42:00 +0000"
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:@"dd-MM-yy hh:mm"];
NSString *dateString = [dateFormat stringFromDate:date];
dateString只是NIL
有谁知道什么是错的?
编辑:我真正想要实现的只是:
NSString *receivedDate = @"2012-02-02T20:42:00+01:00";
NSString *fixedDate = [do some magic]
//value of fixedDate is now: "02-02-12 20:42"
答案 0 :(得分:3)
正如Anna所说,你需要分配一个NSDateFormatter的实例,因为你得到的只是一个NULL ptr,它只是忽略了消息setDateFormat和stringFromDate,而是留下了NULL。
但你的格式是几个小时是不正确的。请参阅Date Formatter reference
这对我有用:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@@"dd-MM-yy HH:mm"];
NSString *dateString = [df stringFromDate:date];