我想将服务器UTC时间转换为本地时间,反之亦然。 这是我的代码..
var isTimeFromServer = true
var time:String!
var period:String!
let timeString = "6:59 AM" //Current UTC time
if isTimeFromServer {
let index = timeString.index(timeString.startIndex, offsetBy: 5)
let twelve = timeString.substring(to: index)
var dateString:String!
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "H:mm"
let date12 = dateFormatter.date(from: twelve)!
dateFormatter.dateFormat = "h:mm a"
let date22 = dateFormatter.string(from: date12)
//print(date22)
dateString = date22
//print("dateString=\(dateString)")
time = dateString.components(separatedBy: " ")[0]
period = dateString.components(separatedBy: " ")[1]
}
else {
time = timeString.components(separatedBy: " ")[0]
period = timeString.components(separatedBy: " ")[1]
}
var hour = Int(time.components(separatedBy: ":")[0])
hour = period == "AM" ? hour : hour! + 12
let minute = Int(time.components(separatedBy: ":")[1])
let calender = NSCalendar.current
var datecomponent = DateComponents()
datecomponent.calendar = calender
datecomponent.hour = hour
datecomponent.minute = minute
if !isTimeFromServer {
// local to UTC
datecomponent.timeZone = TimeZone.current
}
else {
datecomponent.timeZone = TimeZone(abbreviation: "UTC")
}
let date = datecomponent.date
let dateFormatter = DateFormatter()
if !isTimeFromServer {
dateFormatter.dateFormat = "H:mm"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.string(from: date!)
}
else {
//UTC to local
dateFormatter.dateFormat = "h:mm a"
dateFormatter.timeZone = TimeZone.current
dateFormatter.string(from: date!)
}
我得到当地时间
o / p:" 12:52 PM"
但实际的当地时间和输出时差是23分钟。
答案 0 :(得分:64)
我不知道你的代码有什么问题。
但看起来有太多不必要的东西,比如你正在设置日历,从字符串中获取一些元素。
这是我的小版本的UTCToLocal和localToUTC函数。
但是为此您需要以特定格式传递字符串。因为我已经解开了日期对象。但是你可以使用一些保护条件来防止你的应用程序崩溃。
func localToUTC(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.calendar = NSCalendar.current
dateFormatter.timeZone = TimeZone.current
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "H:mm:ss"
return dateFormatter.string(from: dt!)
}
func UTCToLocal(date:String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "H:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "h:mm a"
return dateFormatter.string(from: dt!)
}
并调用以下功能。
print(UTCToLocal(date: "13:07:00"))
print(localToUTC(date: "06:40 PM"))
希望这会对你有所帮助。
快乐的编码!!
答案 1 :(得分:16)
Mrugesh的答案是完美的,但如果有人需要使用他们自己的格式,或者使用某种不同的格式,我会将其推广,以便您可以在两个参数中给出不同的格式或相同的格式。
func localToUTC(date:String, fromFormat: String, toFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = fromFormat
dateFormatter.calendar = NSCalendar.current
dateFormatter.timeZone = TimeZone.current
dateFormatter.date
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: dt!)
}
func UTCToLocal(date:String, fromFormat: String, toFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = fromFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: date)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = toFormat
return dateFormatter.string(from: dt!)
}
let localDateAsString = UTCToLocal(date: dateAsString!, fromFormat: "hh:mm a, dd MMM yyyy", toFormat: "hh:mm a, dd MMM yyyy")
您可以像上面一样使用它。希望它有所帮助。
答案 2 :(得分:7)
借助Mrugesh Tank Answer,
我已经更新了他的答案,并为该日期创建了扩展名。这样您就可以轻松地从ViewController或单元类的任何位置访问函数。
extension String {
//MARK:- Convert UTC To Local Date by passing date formats value
func UTCToLocal(incomingFormat: String, outGoingFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = incomingFormat
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
let dt = dateFormatter.date(from: self)
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = outGoingFormat
return dateFormatter.string(from: dt ?? Date())
}
//MARK:- Convert Local To UTC Date by passing date formats value
func localToUTC(incomingFormat: String, outGoingFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = incomingFormat
dateFormatter.calendar = NSCalendar.current
dateFormatter.timeZone = TimeZone.current
let dt = dateFormatter.date(from: self)
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = outGoingFormat
return dateFormatter.string(from: dt ?? Date())
}
}
示例用法:-
Note:- eventStartDate is the string which you have to converted in your format like this:- "2018-07-11T16:22:00.000Z"
let finalDate = eventStartDate.UTCToLocal(incomingFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", outGoingFormat: "MMM d, yyyy h:mm a")
答案 3 :(得分:1)
请尝试一下:
func convertUTCToLocal(timeString: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
let timeUTC = dateFormatter.date(from: timeString)
if timeUTC != nil {
dateFormatter.timeZone = NSTimeZone.local
let localTime = dateFormatter.string(from: timeUTC!)
return localTime
}
return nil
}
func convertLocalToUTC(localTime: String) -> String? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "h:mm a"
dateFormatter.timeZone = NSTimeZone.local
let timeLocal = dateFormatter.date(from: localTime)
if timeLocal != nil {
dateFormatter.timeZone = TimeZone.init(abbreviation: "UTC")
let timeUTC = dateFormatter.string(from: timeLocal!)
return timeUTC
}
return nil
}
var isTimeFromServer = true
var time:String!
var period:String!
let timeString = "6:59 AM" //Current UTC time
if isTimeFromServer {
print(convertUTCToLocal(timeString: timeString))
} else {
print(convertLocalToUTC(localTime: timeString))
}
答案 4 :(得分:0)
对于每个使用TimeZone对象的人。 我建议你在有可能时从标识符而不是缩写创建TimeZone。
这可以防止夏令时造成的错误。
为了说明我的观点,我们举个例子。
你可以像这样实例化
let timeZone = TimeZone(identifier: "Europe/Paris")
或者那样
let timeZone = TimeZone(abbreviation: "CEST")
或“UTC +2:00”
但这是夏天的时区 CEST 意味着中欧夏令时 我们 CET 意味着中欧时间冬天是“UTC +1:00”
您可以使用Date.isDaylightSavingsTime自行管理夏令时,但这意味着更多代码,您无法控制夏令时的来源。 “从官方文档
”指示接收方当前是否正在使用夏令时所有这些都是为了支持TimeZone(标识符:...)
答案 5 :(得分:0)
您可以快速使用4/5
var myDate:String = "2020-02-18 14:30:57"
var convertedLocalTime:String = ""
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd H:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
if let dt = dateFormatter.date(from: myDate) {
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "yyyy-MM-dd h:mm a"
convertedLocalTime = dateFormatter.string(from: dt)
} else {
print("There was an error decoding the string")
}
print("convertedLocalTime--",convertedLocalTime)
答案 6 :(得分:0)
如果您需要转换时间戳,可以像下面这样使用timezoneOffset:
if let dateStr = model.date, let dateInt = Int(dateStr) {
let timezoneOffset = TimeZone.current.secondsFromGMT()
let localDateInt = dateInt + timezoneOffset
let date = Date(timeIntervalSince1970: Double(localDateInt))
cell.dateLbl.text = date.toShortDateTimeString()
}
答案 7 :(得分:0)
要将 UTC 日期转换为本地系统日期,您可以使用以下日期扩展名:
extension Date {
// Convert local time to UTC (or GMT)
func toGlobalTime() -> Date {
let timezone = TimeZone.current
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self))
return Date(timeInterval: seconds, since: self)
}
// Convert UTC (or GMT) to local time
func toLocalTime() -> Date {
// 1) Get the current TimeZone's seconds from GMT. Since I am in Chicago this will be: 60*60*5 (18000)
let timezoneOffset = TimeZone.current.secondsFromGMT()
// 2) Get the current date (GMT) in seconds since 1970. Epoch datetime.
let epochDate = self.timeIntervalSince1970
// 3) Perform a calculation with timezoneOffset + epochDate to get the total seconds for the
// local date since 1970.
// This may look a bit strange, but since timezoneOffset is given as -18000.0, adding epochDate and timezoneOffset
// calculates correctly.
let timezoneEpochOffset = (epochDate + Double(timezoneOffset))
// 4) Finally, create a date using the seconds offset since 1970 for the local date.
return Date(timeIntervalSince1970: timezoneEpochOffset)
}
}