title: Date to milliseconds and back to date in Swift
body: <p>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.</p>
  <p>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.</p>
  <pre><code>//Date to milliseconds
  func currentTimeInMiliseconds() -&gt; Int! {
      let currentDate = NSDate()
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = format
      dateFormatter.timeZone = NSTimeZone(name: &quot;UTC&quot;) 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) -&gt; 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 ) )!
      }
  }
  </code></pre>
  <p>The timestamp is correct but the date returned isn't.</p>