我可以獲取各個日期組件:如何將每個日期組件特定的日期格式轉換迅速在2
let dateString = String(dataModel.dateCompletedDay) + String(dataModel.dateCompletedMonth) + String(dataModel.dateCompletedYear)
print(dateString)
//2532016
現在我怎樣才能將其轉換爲DD/MM/YYYY格式?
我可以獲取各個日期組件:如何將每個日期組件特定的日期格式轉換迅速在2
let dateString = String(dataModel.dateCompletedDay) + String(dataModel.dateCompletedMonth) + String(dataModel.dateCompletedYear)
print(dateString)
//2532016
現在我怎樣才能將其轉換爲DD/MM/YYYY格式?
我建議使用Calendar
,DateComponents
和DateFormatter
(SWIFT 3)
let components = DateComponents(year:dataModel.dateCompletedYear, month: dataModel.dateCompletedMonth, day: dataModel.dateCompletedDay)
let calendar = Calendar(identifier: .gregorian)!
let date = calendar.date(from: components)!
let formatter = DateFormatter()
formatter.dateFormat = "dd/MM/yyyy"
let dateString = formatter.string(from: date)
此外,這是這是應該簡單的解決方案,也是在斯威夫特工作2
let dateString = String(format: "%02ld/%02ld/%ld", dataModel.dateCompletedDay, dataModel.dateCompletedMonth, dataModel.dateCompletedYear)
使用此'讓dateString = String(dataModel.dateCompletedDay)+「/」+ String(dataModel.dateCompletedMonth)+「/」+ String(dataModel.dateCompletedYear)' –
你想在日期或字符串formate? – Hitesh
StringFormat ... –