title: Converting Timestamp from Firebase to Time Ago
body: <p>When a user clicks on a post from the feed, I want to display the time that each comment was posted. I am trying to convert the Date().timeIntervalSince1970 value to "x time ago" for when it was posted.</p>
  
  <p>Before view loads, I am declaring a now variable for the date today.</p>
  
  <pre><code>let now = Date()
  </code></pre>
  
  <p>The date is assigned when I set up the dictionary for the comments like. </p>
  
  <pre><code>let commentValues: [String: Any] = [
          "comment" : comment,
          "uid": uid,
          "commentDate": Date().timeIntervalSince1970
      ]
  </code></pre>
  
  <p>I am fetching the comments like this where I also call the date function and assign it to the date label in storyboard. </p>
  
  <pre><code>var comments = [Comment]()
  fileprivate func fetchComments() {
  
      guard let postID = self.post?.id else {return}
      let commentRef = Database.database().reference().child("comments").child(postID)
      commentRef.observe(.childAdded, with: { (snapshot) in
  
          guard let dict = snapshot.value as? [String: Any] else {return}
          guard let comment = dict["comment"] as? String else {return}
          guard let uid = dict["uid"] as? String else {return}
          guard let commentDate = dict["commentDate"] as? Double else {return}
  
  
          let userRef = Database.database().reference().child("users/\(String(describing: uid))/profile")
          userRef.observe(.value, with: { snapshot in
              guard let value = snapshot.value as? [String:Any] else {return}
              let username = value["username"]
              let uid = dict["uid"]
              let email = value["email"]
  
              let myTimeInterval = TimeInterval(commentDate)
              let time = Date(timeIntervalSinceNow: myTimeInterval)
  
              let userProfile = User(uid: uid as! String, username: username as! String, email: email as! String)
              let comment = Comment(uid: uid as! String, user: userProfile, comment: comment, time: time.timeAgoDisplay())
              self.comments.append(comment)
              self.commentCollectionView.reloadData()
              })
          })
  }
  </code></pre>
  
  <p>I have set up a date extension where I am trying to calculate the timeAgo for the date: </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
          let month = 4 * week
          let year = 12 * month
  
          if secondsAgo &lt; minute  {
              print("\(secondsAgo)s ago")
              return "\(secondsAgo)s ago"
          } else if secondsAgo &lt; hour {
              return "\(secondsAgo)m ago"
          } else if secondsAgo &lt; day {
              return "\(secondsAgo)hr ago"
          } else if secondsAgo &lt; week {
              return "\(secondsAgo)d ago"
          } else if secondsAgo &lt; month {
              return "\(secondsAgo)w ago"
          } else if secondsAgo &lt; year {
              return "\(secondsAgo)mo ago"
          }
          return "\(secondsAgo)yr ago"
  }
  }
  </code></pre>
  
  <p>The date is being displayed but I'm getting the result as "-12983918s ago". How do I display the time as "2s ago" or "1h ago" or "1w ago" etc?</p>