如何在Swift 3中将CoreData Date转换为DateTime

时间:2017-02-01 12:36:32

标签: ios swift string nsdate

我有CoreData myDate = date,Entity列并返回输出:

let mydate = coreDataValues[0].myDate!
print(mydate)

OUTPUT == 2017-02-01 12:45:40 +0000 

我想将mydate转换为datetime,如下所示,例如

2017-12-20-23-59-59

我该怎么做?

1 个答案:

答案 0 :(得分:5)

你可以这样做:

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
var date = dateFormatter.string(from: mydate as Date)
print(date)
  

OUTPUT = 2017-02-01-18-10-01