Date to milliseconds and back to date in Swift

后端 未结 9 2066
深忆病人
深忆病人 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:14

    let dateTimeStamp = NSDate(timeIntervalSince1970:Double(currentTimeInMiliseconds())/1000)  //UTC time  //YOUR currentTimeInMiliseconds METHOD
    let dateFormatter = NSDateFormatter()
    dateFormatter.timeZone = NSTimeZone.localTimeZone() 
    dateFormatter.dateFormat = "yyyy-MM-dd"
    dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
    
    
    let strDateSelect = dateFormatter.stringFromDate(dateTimeStamp)
    print("Local Time", strDateSelect) //Local time
    
    
    let dateFormatter2 = NSDateFormatter()
    dateFormatter2.timeZone = NSTimeZone(name: "UTC") as NSTimeZone!
    dateFormatter2.dateFormat = "yyyy-MM-dd"
    
    let date3 = dateFormatter.dateFromString(strDateSelect)
    print("DATE",date3)
    

提交回复
热议问题