let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd:MM:yyy hh:mm:ss"
let timeZone = TimeZone(identifier: "Europe/Amsterdam")
dateFormatter.timeZone = timeZone
dateFormatter.string(from: Date())
Run Code Online (Sandbox Code Playgroud)
虽然,我认为格式"dd-MM-yyyy HH:mm:ss"似乎更正确。话说回来; 设置dateFormat应该用于解析一个自定义的日期格式时设置的格式。当您格式化日期以显示给用户时,您应该使用用户期望的日期格式(在默认系统区域设置中)。为此,您应该使用dateStyleand timeStyle:
dateFormatter.dateStyle = .medium
print(dateFormatter.string(from: Date())) // prints "Oct 28, 2017"
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
print(dateFormatter.string(from: Date())) // prints "Oct 28, 2017 Oct 28, 2017 at 11:19:13 PM"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5677 次 |
| 最近记录: |