title: Convert Firebase Firestore Timestamp to Date (Swift)?
body: <p>I have a date saved in a Firestore field as a timestamp that I want to convert to a Date in Swift:</p>
  
  <p><code>June 19,2018 at 7:20:21 PM UTC-4</code></p>
  
  <p>I tried the following but I get an error:</p>
  
  <p><code>let date = Date(timeIntervalSince1970: postTimestamp as! TimeInterval)</code></p>
  
  <p>Error:</p>
  
  <p><code>Could not cast value of type 'FIRTimestamp' (0x104fa8b98) to 'NSNumber'</code></p>
  
  <p>The reason why I want to convert to Date is so that I can use this Date extension to mimic timestamps you see on Instagram posts:</p>
  
  <pre><code>extension Date {
      func timeAgoDisplay() -&gt; String {
          let secondsAgo = Int(Date().timeIntervalSince(self))
  
          let minute = 60
          let hour = 60 * minute
          let day = 24 * hour
          let week = 7 * day
  
          if secondsAgo &lt; minute {
              return "\(secondsAgo) seconds ago"
          } else if secondsAgo &lt; hour {
              return "\(secondsAgo / minute) minutes ago"
          } else if secondsAgo &lt; day {
              return "\(secondsAgo / hour) hours ago"
          } else if secondsAgo &lt; week {
              return "\(secondsAgo / day) days ago"
          }
  
          return "\(secondsAgo / week) weeks ago"
      }
  }
  </code></pre>