简体   繁体   English

dateFromString不断返回nil

[英]dateFromString keeps returning nil

- (NSDate*) parseTime:(NSString*) time
{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"MMMM dd, yyyy hh:mm a"];

    NSDate* date = [dateFormatter dateFromString:time];

    return date;
}

Where time = "08 12, 2014 08:00 PM " and it is returning nil. 时间=“ 08 12,2014 08:00 PM”,返回零。

I'm new to this and have tried searching around on everything including Apples help docs, but could use a little more help here to finish pointing me in the right direction please. 我对此并不陌生,并已尝试搜索包括Apple帮助文档在内的所有内容,但可以在此处使用更多帮助以完成指向正确方向的过程。

Try adjusting like the following: 尝试如下调整:

[dateFormatter setDateFormat:@"MM dd, yyyy hh:mm a"];

And dont forget to set the timezone on your formatter to avoid unexecpted result, for example: 并且不要忘记在格式化程序上设置时区,以免产生未执行的结果,例如:

dateFormatter.timeZone  = [NSTimeZone timeZoneWithAbbreviation:@"EDT"];

Very Important link by posted by @CrimsonChris: @CrimsonChris发布的非常重要的链接:

LOCALE DATA MARKUP LANGUAGE (LDML) 本地数据标记语言(LDML)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM