Swift 3.0:将服务器UTC时间转换为本地时间,反之亦然

iaqfqrcu  于 2022-12-28  发布在  Swift
关注(0)|答案(8)|浏览(123)

我想转换服务器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”
但实际本地时间和输出时间相差23分钟。

cnwbcb6i

cnwbcb6i1#

我不知道你的密码有什么问题。
但是看起来有太多不必要的东西,比如你在设置日历,从字符串中获取一些元素,这是我的UTCToLocal和localToUTC函数的小版本。
但是你需要传递特定格式的字符串,因为我已经强制打开了日期对象,但是你可以使用一些保护条件来防止你的应用崩溃。

func localToUTC(dateStr: String) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "h:mm a"
    dateFormatter.calendar = Calendar.current
    dateFormatter.timeZone = TimeZone.current
    
    if let date = dateFormatter.date(from: dateStr) {
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        dateFormatter.dateFormat = "H:mm:ss"
    
        return dateFormatter.string(from: date)
    }
    return nil
}

func utcToLocal(dateStr: String) -> String? {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "H:mm:ss"
    dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
    
    if let date = dateFormatter.date(from: dateStr) {
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.dateFormat = "h:mm a"
    
        return dateFormatter.string(from: date)
    }
    return nil
}

并如下调用这些函数。

print(utcToLocal(dateStr: "13:07:00"))
print(localToUTC(dateStr: "06:40 PM"))
1szpjjfi

1szpjjfi2#

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")

你可以像上面一样使用它。希望它有帮助。

42fyovps

42fyovps3#

在Mrugesh Tank Answer的帮助下
我已经更新了他的答案,并为日期创建了扩展。这样你就可以很容易地从任何地方访问这些函数,无论是从ViewController还是从Cell类。

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")
wnvonmuf

wnvonmuf4#

要将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)
    }

}
llmtgqce

llmtgqce5#

对于每个使用时区对象的人。我建议你在可能的情况下从标识符而不是缩写创建时区。
这样可以防止夏令时引起的错误。
为了说明我的观点,让我们举一个例子。你可以像这样示例化let timeZone = TimeZone(identifier: "Europe/Paris")或者像这样示例化let timeZone = TimeZone(abbreviation: "CEST")或者“UTC +2:00”
但这是夏季时区CEST表示中欧夏季时间我们有CET表示中欧时间表示冬季,即“UTC +1:00”
您可以使用Date.isDaylightSavingsTime管理自己的夏令时,但这意味着更多的代码,并且您无法控制夏令时的来源。“指示接收器当前是否使用夏令时”来自官方文档
所有的都是那就是说偏爱时区(标识符:...)

0s7z1bwu

0s7z1bwu6#

您可以在swift 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)
unftdfkk

unftdfkk7#

请尝试:

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))
    }
fruv7luv

fruv7luv8#

如果您需要转换时间戳,可以使用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()
        }

相关问题