Converting String to NSDate Giving Wrong Date

后端 未结 2 487
清歌不尽
清歌不尽 2020-12-12 07:19

I have a String that I converted using stringFromDate and now I\'m trying to convert it back, however when the UIPicker starts, it\'s giving me the wrong day

相关标签:
2条回答
  • 2020-12-12 08:02

    You are using the wrong format. You need dd MMM yyyy.

    0 讨论(0)
  • 2020-12-12 08:05

    To use correct format string is most important..

    YYYY is week-based calendar year. (used in ISO week-year calendar)

    yyyy is ordinary calendar year.

    so, You should use 'yyyy' instead of 'YYYY'.

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy"
    
    print(birthday)  
    
    let date = dateFormatter.dateFromString(birthday)
    self.datePicker.setDate(date!, animated: true)
    

    For more string format for Date: refer this link

    https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx

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