Date to milliseconds and back to date in Swift

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 time to nanoseconds and then back to a date string but the time gets convoluted when I go from a string to date.

    //Date to milliseconds
     func currentTimeInMiliseconds() -> Int! {
            let currentDate = NSDate()
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = format
            dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
            let date = dateFormatter.date(from: dateFormatter.string(from: currentDate as Date))
            let nowDouble = date!.timeIntervalSince1970
            return Int(nowDouble*1000)
        }

    //Milliseconds to date
    extension Int {
        func dateFromMilliseconds(format:String) -> Date {
            let date : NSDate! = NSDate(timeIntervalSince1970:Double(self) / 1000.0)
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = format
            dateFormatter.timeZone = TimeZone.current
            let timeStamp = dateFormatter.string(from: date as Date)

let formatter = DateFormatter()
            formatter.dateFormat = format
            return ( formatter.date( from: timeStamp ) )!
        }
    }

//The timestamp is correct but the date returned isn’t

  • Table View Cells jump when selected on iOS 8
  • Install iOS app directly from website - no password, no iTunes, no UDID
  • Protecting the app sandbox
  • How can I regenerate ios folder in React Native project?
  • Xcode 6 Interface Builder “cannot open compiled nibs” error, FiksuSDK
  • Debug iOS 6+7 Mobile Safari using the Chrome DevTools
  • 4 Solutions Collect From Internet About “Date to milliseconds and back to date in Swift”

    I don’t understand why you’re doing anything with strings…

    extension Date {
        var millisecondsSince1970:Int {
            return Int((self.timeIntervalSince1970 * 1000.0).rounded())
        }
    
        init(milliseconds:Int) {
            self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
        }
    }
    
    
    Date().millisecondsSince1970 // 1476889390939
    Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC)
    

    As @Travis Solution works but in some cases

    var millisecondsSince1970:Int WILL CAUSE CRASH APPLICATION ,

    with error

    Double value cannot be converted to Int because the result would be greater than Int.max if it occurs Please update your answer with Int64

    Here is Updated Answer

    extension Date {
     var millisecondsSince1970:Int64 {
            return Int64((self.timeIntervalSince1970 * 1000.0).rounded()) 
            //RESOLVED CRASH HERE
        }
    
        init(milliseconds:Int) {
            self = Date(timeIntervalSince1970: TimeInterval(milliseconds / 1000))
        }
    }
    

    Hope it is helpful to someone who also has same problem

    //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 !.

    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)