NSDateFormatter 返回 null

发布于 2024-11-09 11:32:35 字数 406 浏览 0 评论 0原文

我正在尝试将 NSDate 格式化为日期格式为 yyyyMMdd 的字符串,这是

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];

NSDate *date =data.createdTime;

NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);

NSLog 返回我这个值的

Tue, 24 May 2011 0:05:01 +0800
(null)

代码有人知道哪一部分是错误的吗?

提前致谢。

I'm trying to format a NSDate to a string with date format of yyyyMMdd, here's the code

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];

NSDate *date =data.createdTime;

NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);

the NSLog return me this value

Tue, 24 May 2011 0:05:01 +0800
(null)

Anyone know which part is wrong?

Thanx in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度℉ 2024-11-16 11:32:35

也许currentArticle.createdTime是一个有效的日期,但data.createdTimenil?此外,您正在对 dateFormat 执行 -setDateFormat: 选择器,但您的日期格式化程序似乎是 dateFormatter

尝试以下代码:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyyMMdd"];

NSDate *date = data.createdTime;

NSLog(@"   Normal Date = %@", date);
NSLog(@"Formatted Date = %@", [dateFormatter stringFromDate: date]);

[dateFormatter release];

如果 datenil,则两个 NSLog() 调用都会告诉您。


编辑

仔细检查 data.createdTime 是一个 NSDate 实例。也许它是另一个类的实例,例如 NSString,其 -description 返回显示的日期。这可以解释为什么 NSLog()“显示”日期,但格式化程序返回 nil。

BOOL isDate = [data.createdTime isKindOfClass: [NSDate class]];
NSLog(@"Date %@ a date.", isDate ? @"is" : @"is not");

Perhaps currentArticle.createdTime is a valid date but data.createdTime is nil? In addition, you are performing the -setDateFormat: selector on dateFormat, but it seems like your date formatter is dateFormatter.

Try the following code:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: @"yyyyMMdd"];

NSDate *date = data.createdTime;

NSLog(@"   Normal Date = %@", date);
NSLog(@"Formatted Date = %@", [dateFormatter stringFromDate: date]);

[dateFormatter release];

If date is nil, then both NSLog() calls will tell you.


Edit

Double check that data.createdTime is an NSDate instance. Perhaps it is an instance of another class, such as NSString, whose -description returns the displayed date. This would explain why NSLog() “shows” the date, but the formatter is returning nil.

BOOL isDate = [data.createdTime isKindOfClass: [NSDate class]];
NSLog(@"Date %@ a date.", isDate ? @"is" : @"is not");
慕巷 2024-11-16 11:32:35
[dateFormat setDateFormat:@"yyyyMMdd"];

应该是:

[dateFormatter setDateFormat:@"yyyyMMdd"];

如果它仍然不起作用,那么 data.createdTime; 出了问题,因为我运行了这段代码并收到了预期的输出:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];

NSDate *date = [NSDate date];

NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);
[dateFormat setDateFormat:@"yyyyMMdd"];

Should be:

[dateFormatter setDateFormat:@"yyyyMMdd"];

If it still isn't working then something is wrong with data.createdTime; because I ran this code and recieved the expected output:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMdd"];

NSDate *date = [NSDate date];

NSLog(@"%@",date);
NSLog(@"%@",[dateFormatter stringFromDate:date]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文