title: iOS Calendar Format "days ago", "weeks ago", etc... not formatting correctly Swift
body: <p>I'm loading data from Firebase Firestore into a table view and my cell.timeLabel.text needs to display the date as __ seconds ago (or if greater __ minutes ago, all the way to years ago). My current code isn't working and I even simulated a date that should return seconds ago but it said 12 hours for some reason. What's wrong and how do I solve this?</p>
  <p>Extension Code:</p>
  <pre><code>extension Date {
      func years(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
      }
      func months(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
      }
      func weeks(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
      }
      func days(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
      }
      func hours(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
      }
      func minutes(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
      }
      func seconds(from date: Date) -&gt; Int {
          return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
      }
      func offset(from date: Date) -&gt; String {
          if years(from: date)   == 1 { return &quot;\(years(from: date)) year ago&quot;   } else { if years(from: date)   &gt; 1 { return &quot;\(years(from: date)) years ago&quot;   } }
          if months(from: date)  == 1 { return &quot;\(months(from: date)) month ago&quot;  } else { if months(from: date)  &gt; 1 { return &quot;\(months(from: date)) months ago&quot;  } }
          if weeks(from: date)   == 1 { return &quot;\(weeks(from: date)) week ago&quot;   } else { if weeks(from: date)   &gt; 1 { return &quot;\(weeks(from: date)) weeks ago&quot;   } }
          if days(from: date)    == 1 { return &quot;\(days(from: date)) day ago&quot;    } else { if days(from: date)    &gt; 1 { return &quot;\(days(from: date)) days ago&quot;    } }
          if hours(from: date)   == 1 { return &quot;\(hours(from: date)) hour ago&quot;   } else { if hours(from: date)   &gt; 1 { return &quot;\(hours(from: date)) hours ago&quot;   } }
          if minutes(from: date) == 1 { return &quot;\(minutes(from: date)) minute ago&quot; } else { if minutes(from: date) &gt; 1 { return &quot;\(minutes(from: date)) minutes ago&quot; } }
          if seconds(from: date) == 1 { return &quot;\(seconds(from: date)) second ago&quot; } else { if seconds(from: date) &gt; 1 { return &quot;\(seconds(from: date)) seconds ago&quot; } }
          return &quot;&quot;
      }
  }
  </code></pre>
  <p>TableView Code:</p>
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &quot;MainTableViewCell&quot;, for: indexPath) as! MainTableViewCell
      let date1 = DateComponents(calendar: .current, year: payment.year, month: payment.month, day: payment.day, hour: payment.hour, minute: payment.minute, second: payment.second).date!
      let timeOffset = now.offset(from: date1)
      cell.timeLabel.text = timeOffset
  }
  </code></pre>