我想根据我的 iPhone 设备获取当前时间,我正在使用昨天给我的代码。 我的代码是
let calendar = Calendar.current
let dateStart = calendar.startOfDay(for: Date())
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.timeZone = TimeZone.current
let strDate = Date().description(with: Locale.current) //dateFormatter.string(from: dateStart)
print("日期开始***** (dateStart)")
通过这个我得到了回应:
开始日期 ***** 2018-02-14 18:30:00 +0000
任何人都可以帮助我如何确定今天的正确时间是 15-02-2018 18:00
谢谢
最佳答案
试试这个:
let calendar = Calendar.current
let dateStart = calendar.startOfDay(for: Date()) // Feb 15, 2018 at 12:00 AM"
print(dateStart) // 2018-02-14 18:30:00 +0000
let dateFormatter = DateFormatter()
var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "UTC" }
dateFormatter.locale = Locale.init(identifier: localTimeZoneAbbreviation)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
let newDate = dateFormatter.string(from: dateStart)
print(newDate) // 2018-02-15 00:00
更新:
// Current Time
dateStart = Date()
newDate = dateFormatter.string(from: dateStart)
print(newDate) // 2018-02-15 22:30
关于ios - 根据设备位置印度或美国更正日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48807379/