1

我让这段代码工作了一段时间,但它停止了工作,而且这总是返回 null,尽管日期是有效的。这是代码和输出:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm dd.MM.yyyy"];
    NSLog(@"date - %@, formatted date - %@", self.date, [dateFormatter stringFromDate:self.date]);

date - 2013-09-02 10:00:00, formatted date - (null)
4

2 回答 2

1

这个对我有用。

输出:

2013-09-04 10:51:10.681 Untitled[753:707] date - 2013-09-04 08:51:10 +0000, formatted date - 10:51 04.09.2013

我用了:

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

所以你的self.date一定有什么东西。您可以使用 self.date 解释或粘贴更多代码。

于 2013-09-04T08:51:52.127 回答
1
Try like this:-
The problem is your self.date was not converting it into proper date format:
here assuming your self.date was string object

    NSDate *convertIntoDate=[NSDate dateWithNaturalLanguageString:self.date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm dd.MM.yyyy"];
    NSLog(@"date - %@, formatted date - %@", self.date, [dateFormatter stringFromDate:convertIntoDate]);
于 2013-09-04T09:51:52.303 回答