NSDateFormatter返回null,但之前工作正常

时间:2013-09-04 08:47:47

标签: ios objective-c cocoa

我让这段代码工作了一段时间,但它停止了工作,虽然日期有效,但它总是返回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)

2 个答案:

答案 0 :(得分: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解释或粘贴更多代码。

答案 1 :(得分: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]);