Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSDateFormatter返回零_Objective C_Nsdateformatter - Fatal编程技术网

Objective c NSDateFormatter返回零

Objective c NSDateFormatter返回零,objective-c,nsdateformatter,Objective C,Nsdateformatter,有人能告诉我为什么此代码段在我的日志文件中返回nil: NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"]; NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] ini

有人能告诉我为什么此代码段在我的日志文件中返回nil:

    NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];

NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-mm-dd HH:MM"];
[dateFormatter setLocale:englishLocale];

NSLog(@"%@",[dateFormatter dateFromString:dateString]);

非常感谢。

您应该将“HH:MM”改为“HH:MM”

NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];

    NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //HH:MM -> HH:mm
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    [dateFormatter setLocale:englishLocale];

    NSLog(@"%@",[dateFormatter dateFromString:dateString]);
检查我的变体:

NSString *dateString = [[NSString alloc] initWithString: @"2011-08-23 15:00"];

NSLocale *englishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//HH:MM -> HH:mm
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[dateFormatter setLocale:englishLocale];

NSLog(@"%@",[dateFormatter dateFromString:dateString]);
如您所见,我已将日期格式更改为:
@“yyy-MM-dd HH:MM”

所有格式都可以在这里找到:


关于更多的日期格式

这是因为NSLocale设置吗?现在是夏天,生活很轻松。。。(即是)对不起。我搞错了。我也修改了。