简体   繁体   English

NSDateFormatter dateFromString 总是返回 nil

[英]NSDateFormatter dateFromString Always Returns nil

I want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet.我想提前为问一个重复的问题道歉,但其他解决方案都没有对我有用。 Every time I try to pass a date string to the dateFromString function I get nil.每次我尝试将日期字符串传递给 dateFromString 函数时,我都会得到零。 I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.我没有读到任何自 iOS 7 更新以来情况发生变化的地方,但我正在关注更新,如果这对这是否仍然以相同的方式工作有影响的话。

This is the code I'm using to create the date from string:这是我用来从字符串创建日期的代码:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormat setLocale:[NSLocale systemLocale]];
    [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    [dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];

    NSDate *date = [[NSDate alloc] init];
    date = [dateFormat dateFromString:dateString];

    return date;

I've set up my dateFormat based on all the solutions I've read to solve this problem, but none of these settings have solved by problem.我已经根据我读过的所有解决方案设置了我的 dateFormat 来解决这个问题,但是这些设置都没有解决问题。 The systemLocale is definitely set up for English so that should not be causing any issues. systemLocale 绝对是为英语设置的,所以应该不会造成任何问题。

This is the dateString I'm passing to dateFromString:这是我传递给 dateFromString 的 dateString:

Wednesday, October 9, 2013 at 2:40:29 PM Pacific Daylight Time

Thanks for the help!感谢您的帮助!

There are two issues here:这里有两个问题:

  1. The format of date string the formatter is expecting ( @"yyyy-MM-dd hh:mm:ss" ) is different from the format of the date string you're trying to parse ( @"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz" ).格式化程序期望的日期字符串格式 ( @"yyyy-MM-dd hh:mm:ss" ) 与您尝试解析的日期字符串格式不同 ( @"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz" )。

  2. Setting the formatter's locale to [NSLocale systemLocale] is causing [dateFormat dateFromString:] to return nil .将格式化程序的语言环境设置为[NSLocale systemLocale]会导致[dateFormat dateFromString:]返回nil Set it to [NSLocate currentLocale] .将其设置为[NSLocate currentLocale]

The full code for the formatter should be:格式化程序的完整代码应该是:

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormat setLocale:[NSLocale currentLocale]];
    [dateFormat setDateFormat:@"EEEE, MMMM d, yyyy 'at' h:mm:ss a zzzz"];
    [dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];

    NSDate *date = [dateFormat dateFromString:dateString];

Yet another way to get nil is if you use hh and your hours are on the 24 hr clock and > 12, in that case, you need HH (or H , for zero-padded).另一种获得 nil 的方法是,如果您使用hh并且您的工作时间是 24 小时制并且大于 12,那么在这种情况下,您需要HH (或H ,用于零填充)。

That is:即:

  • Format: yyyy-MM-DD hh:mm:ss, string: "2016-03-01 13:42:17" will return nil格式:yyyy-MM-DD hh:mm:ss,字符串:"2016-03-01 13:42:17" 将返回 nil
  • Format: yyyy-MM-DD HH:mm:ss, string: "2016-03-01 13:42:17" will return the date you expect.格式:yyyy-MM-DD HH:mm:ss,字符串:“2016-03-01 13:42:17”将返回您期望的日期。

Hat-tip to @neilco (see comments below his answer) for this.为此向@neilco(见他的回答下方的评论)致谢。 If you like this answer, please up-vote his, too.如果你喜欢这个答案,也请给他点赞。

According to NSDateFormatter documentation :根据NSDateFormatter 文档

When working with fixed format dates, such as RFC 3339, you set the dateFormat property to specify a format string.使用固定格式日期(例如 RFC 3339)时,您可以设置 dateFormat 属性以指定格式字符串。

If your date format is 2017-06-16T17:18:59.082083Z then dateFormat property should look like this yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ .如果您的日期格式为2017-06-16T17:18:59.082083ZdateFormat属性应如下所示yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ

Swift 3斯威夫特 3

let dateFormatter = DateFormatter()
let date = "2017-06-16T17:18:59.082083Z"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"

let result = dateFormatter.date(from: date) // 2017-06-16 17:18:59 +0000

Your date format doesn't match the string that you're passing, your dateString should be in this formate as per your [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"];您的日期格式与您传递的字符串不匹配,您的 dateString 应该按照您的 [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss"] 使用此格式;

2013-10-09 02:40:29 2013-10-09 02:40:29

nil means dateFormat object was unable to parse your string. nil 表示 dateFormat 对象无法解析您的字符串。

In case anybody is stuck on the same hilarious edge case as me:如果有人和我一样被困在同一个热闹的边缘案例中:

"2020-03-08T02:00:00" will return nil as long as you're in a locale that follows Daylight Savings Time, because that hour is skipped and simply doesn't exist.只要您在遵循夏令时的语言环境中,“2020-03-08T02:00:00”就会返回nil ,因为那个小时被跳过并且根本不存在。

You're trying to use dateFromString but the Format you have passed to your Formatter is different of what you're using in dateString.您正在尝试使用 dateFromString,但您传递给格式化程序的格式与您在 dateString 中使用的格式不同。

Try to use this config in your dateFormat: E',' M d',' yyyy 'at' hh:mm:ss aa z尝试在您的 dateFormat 中使用此配置: E',' M d',' yyyy 'at' hh:mm:ss aa z

不要忘记转义"yyyy/MM/dd' 'HH:mm:ss"空格符号

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

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