What is the best way to deal with the NSDateFormatter locale “feechur”?

后端 未结 4 1957
花落未央
花落未央 2020-11-21 05:38

It seems that NSDateFormatter has a \"feature\" that bites you unexpectedly: If you do a simple \"fixed\" format operation such as:

NSDateForma         


        
4条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:28

    May I suggest something totally different because to be honest all of this is somewhat running down a rabbit hole.

    You should be using one NSDateFormatter with dateFormat set and locale forced to en_US_POSIX for receiving dates (from servers/APIs).

    Then you should be using a different NSDateFormatter for the UI which you will set the timeStyle/dateStyle properties - this way you're not having an explicit dateFormat set by yourself, thus falsely assuming that format will be used.

    This means UI is driven by user preferences (am/pm vs 24 hour, and date strings formatted correctly to user choice - from iOS settings), whereas dates that are "coming into" your app are always being "parsed" correctly to an NSDate for you to use.

提交回复
热议问题