NSDateFormatter dateFromString gives the wrong date

前端 未结 2 1989
花落未央
花落未央 2020-12-09 17:25

I\'ve already tried the suggestions in other posts similar to this with no avail. Here is the situation:

I have an NSString like this:

Fri November          


        
相关标签:
2条回答
  • 2020-12-09 17:44

    You need to use yyyy for the year, not YYYY.

    From the Apple reference:

    A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of "Week of Year"), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.

    And to be correct you also have to change EE to EEE. Though they yield the same result in my test.

    0 讨论(0)
  • 2020-12-09 17:53

    I think you need "EEE MMMM d, YYYY" i.e. triple E as format string. If this does not work, I recommend using hard coded strings with easier dates and formats to break down the problem. See for example NSDateFormatter not working for more hints on that topic.

    0 讨论(0)
提交回复
热议问题