Converting NSString to NSDate (and back again)

后端 未结 17 2516
-上瘾入骨i
-上瘾入骨i 2020-11-21 23:26

How would I convert an NSString like \"01/02/10\" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDat

17条回答
  •  一个人的身影
    2020-11-22 00:06

    String To Date

    var dateFormatter = DateFormatter()
    dateFormatter.format = "dd/MM/yyyy"
    
    var dateFromString: Date? = dateFormatter.date(from: dateString) //pass string here
    

    Date To String

     var dateFormatter = DateFormatter()
     dateFormatter.dateFormat = "dd-MM-yyyy"
     let newDate = dateFormatter.string(from: date) //pass Date here
    

提交回复
热议问题