我正在尝试将a转换NSDate为a String,然后更改格式。但是当我传递NSDate给String它时会产生空白。
NSDate
String
let formatter = DateFormatter() let myString = (String(describing: date)) formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let yourDate: Date? = formatter.date(from: myString) formatter.dateFormat = "dd-MMM-yyyy" print(yourDate)
您可以从Apple Dateformatter文档中获取详细信息。如果要为dateString设置dateformat,请参阅此链接,您可以在此处获取详细的dateformat ,例如
let formatter = DateFormatter() // initially set the format based on your datepicker date / server String formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let myString = formatter.string(from: Date()) // string purpose I add here // convert your string to date let yourDate = formatter.date(from: myString) //then again set the date format whhich type of output you need formatter.dateFormat = "dd-MMM-yyyy" // again convert your date to string let myStringafd = formatter.string(from: yourDate!) print(myStringafd)
你得到的输出为