Adding Time Offset in Swift

后端 未结 3 649
一向
一向 2020-12-30 04:22

I have a bunch of different show times in a database and want to display the correct time based on the users time zone by creating an offset.

I\'m getting the users

3条回答
  •  隐瞒了意图╮
    2020-12-30 04:52

    Here is my final code, combined with b.Morgans answer. I believe it's all working now.

    let offsetTime = NSTimeInterval(NSTimeZone.localTimeZone().secondsFromGMT)
    
    var showTimeStr = "05:00 PM" //show time in GMT as String
    let formatter = NSDateFormatter()
    formatter.dateFormat = "h:mm a"
    let showTime = formatter.dateFromString(showTimeStr)
    let finalTime = showTime?.dateByAddingTimeInterval(offsetTime) //Add number of hours in seconds, subtract to take away time
    showTimeStr = formatter.stringFromDate(finalTime!)
    

提交回复
热议问题