我可以获得各个日期组件:
let dateString = String(dataModel.dateCompletedDay) + String(dataModel.dateCompletedMonth) + String(dataModel.dateCompletedYear)
print(dateString)
//2532016
现在我该如何将其转换为DD / MM / YYYY格式?
答案 0 :(得分:5)
我建议使用DateComponents
,DateFormatter
和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)
(Swift 3)
let dateString = String(format: "%02ld/%02ld/%ld", dataModel.dateCompletedDay, dataModel.dateCompletedMonth, dataModel.dateCompletedYear)
此外,这是一个简单的解决方案,应该也适用于Swift 2
lapply