我想将日期“01/12/2017”从字符串转换为日期格式,但在转换后给我这个格式日期2017-12-01 00:00: 00 +0000。 但我只想要这种格式“01/12/2017”的日期。这里是我的代码。从字符串到日期格式的日期更改格式3
func onlyDate(date: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let date1 = dateFormatter.date(from: date)
print(date1!)
return date1!
}
一个日期仅仅是一个时间点。它没有“格式”。您需要使用DateFormatter根据需要显示它。顺便说一句,如果你想要一个时间不敏感的日期,你应该使用中午,而不是设置时区为gmt。 –
可能重复[转换字符串到DATE类型在Swift 3](https://stackoverflow.com/questions/41435857/convert-string-to-date-type-in-swift-3) –
我试过代码从这个链接也没有得到适当的日期。它给了我这个结果“2017-01-01 00:12:00 +0000”。我将格式更改为dateFormatter.dateFormat =“dd/mm/yyyy”。 – manku