我想将服务器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分钟。
我不知道你的代码有什么问题。 但看起来太多不必要的东西,比如你正在设置日历,从字符串中获取一些元素。这是我的小版本的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"))
希望这会帮助你。 快乐的编码!!
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")
你可以像上面一样使用它。希望能帮助到你。
在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")
请试一试:
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))
}
对于每个使用TimeZone对象的人。我建议你在有可能时用标识符而不是缩写创建TimeZone。
这可以防止夏令时造成的错误。
为了说明我的观点,让我们举一个例子。您可以像let timeZone = TimeZone(identifier: "Europe/Paris")
那样实例化,或者像let timeZone = TimeZone(abbreviation: "CEST")
或“UTC +2:00”那样实例化
但这是夏天的时区CEST意思是中欧夏令时我们有CET意思是中欧时间冬天是“UTC +1:00”
您可以使用Date.isDaylightSavingsTime自行管理夏令时,但这意味着更多代码,并且您无法控制夏令时的来源。 “指示接收器当前是否正在使用夏令时”来自官方文档
所有这就是说有利于TimeZone(标识符:......)