将字符串格式日期值转换为日期类型时,时区始终设置为上午 12:00 将“2021-12-04”转换为日期时如何设置为设备的当前时间?
func stringToDateConvertion(date: String, format: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.init(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
dateFormatter.dateFormat = format
if let startDate = dateFormatter.date(from: dateStr) {
return startDate as Date
}
return Date() as Date
}
let dateValue = "2021-12-04"
stringToDateConvertion(date:dateString, format: "YYYY-MM-dd")
结果 dataString 值为
2021-12-04 00:00:00 +0000
- timeIntervalSinceReferenceDate : 660268800.0
如何显示当前时间值“04:37:39 +0000”后修复到日期值。