Date to milliseconds and back to date in Swift

后端 未结 9 2096
深忆病人
深忆病人 2020-11-29 01:53

I am taking the current time, in UTC, and putting it in nanaoseconds and then I need to take the nanoseconds and go back to a date in local time. I am able to do get the tim

9条回答
  •  星月不相逢
    2020-11-29 02:29

    //Date to milliseconds
    func currentTimeInMiliseconds() -> Int {
        let currentDate = Date()
        let since1970 = currentDate.timeIntervalSince1970
        return Int(since1970 * 1000)
    }
    
    //Milliseconds to date
    extension Int {
        func dateFromMilliseconds() -> Date {
            return Date(timeIntervalSince1970: TimeInterval(self)/1000)
        }
    }
    

    I removed seemingly useless conversion via string and all those random !.

提交回复
热议问题