我想将字符串日期(“ 2019-06-28”)转换为日期对象(格式为“ yyyy-MM-dd”)。我的输出是2019-06-28 18:30:00 +0000,但是我不需要18:30:00 +0000。我只想要日期(2019-06-28)。
override func viewDidLoad() {
super.viewDidLoad()
let dateString = "2019-06-28"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dateString)
print(date!)
}
答案 0 :(得分:0)
尝试..
let dateString = "2019-06-28"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: dateString)
let eventDateFormat2 = DateFormatter()
eventDateFormat2.dateFormat = "yyyy-MM-dd"
let dateString2 = eventDateFormat2.string(from: date!)
print(dateString2)