Swift date formatting

后端 未结 1 460
生来不讨喜
生来不讨喜 2020-12-21 00:43

I\'m trying to pull a date string from a button and format is as a date to be store in CoreData.

Here is my code:

let dateStr = setDateBTN.titleLabe         


        
相关标签:
1条回答
  • 2020-12-21 01:46

    NSDateFormatter Class Reference : http://goo.gl/7fp9gl

    Date Formatting Guide (Apple) : http://goo.gl/8zRTQl

    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.

    Your code should work, as you expect, like this :

    let dateStr = setDateBTN.titleLabel?.text
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy"
    let date:NSDate = dateFormatter.dateFromString(dateStr!)!
    
    0 讨论(0)
提交回复
热议问题