NSDateFormatter dateFromString Always Returns nil

前端 未结 7 746
不知归路
不知归路 2020-12-06 10:19

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 dateFro

7条回答
  •  日久生厌
    2020-12-06 10:43

    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).

    That is:

    • Format: yyyy-MM-DD hh:mm:ss, string: "2016-03-01 13:42:17" will return nil
    • Format: yyyy-MM-DD HH:mm:ss, string: "2016-03-01 13:42:17" will return the date you expect.

    Hat-tip to @neilco (see comments below his answer) for this. If you like this answer, please up-vote his, too.

提交回复
热议问题