title: Swift convert time to time ago
body: <p>In Swift5, we have <code>RelativeDateTimeFormatter</code></p>
  
  <p>Prior to Swift5:</p>
  
  <hr>
  
  <p>I'm trying to convert time to time ago, what i wanna do is:</p>
  
  <p>from 1 to 15 seconds it will say " Just now "</p>
  
  <p>from 60 minutes to 119 minutes it will say " an hour ago "</p>
  
  <p>from 24 hours to 47 hours it will say " Yesterday "</p>
  
  <p>from 7 days to 7 days and 23 hours it will say " a week ago "</p>
  
  <p>I'm not sure about my counting if am i wrong feel free to fix it for me</p>
  
  <p>Here is the code</p>
  
  <pre><code>    extension NSDate {
  
      struct Date {
          static let timeFormatter: NSDateFormatter = {
              let df = NSDateFormatter()
              df.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
              df.dateFormat = "h:mm a"
              return df
              }()
      }
      var time: String { return Date.timeFormatter.stringFromDate(self) }
  
      var year:    Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Year,   fromDate: self) }
      var month:   Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Month,  fromDate: self) }
      var day:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Day,    fromDate: self) }
      var hour:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Hour,    fromDate: self) }
      var minute:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Minute,    fromDate: self) }
      var second:     Int { return NSCalendar.autoupdatingCurrentCalendar().component(.Second,    fromDate: self) }
  
  
      struct DateComponents {
          static let formatter: NSDateComponentsFormatter = {
              let dcFormatter = NSDateComponentsFormatter()
              dcFormatter.calendar = NSCalendar(identifier: NSCalendarIdentifierISO8601)!
              dcFormatter.unitsStyle = .Full
              dcFormatter.maximumUnitCount = 1
              dcFormatter.zeroFormattingBehavior = .Default
              dcFormatter.allowsFractionalUnits = false
              dcFormatter.allowedUnits = [.Year, .Month, .Weekday, .Day, .Hour, .Minute, .Second]
              return dcFormatter
              }()
      }
  
      var elapsedTime: String {
  
  
          if isecond(NSDate()) {
              return "Just now"
  
          }
  
          if ishour(NSDate()) {
             return "an hour ago"
  
          }
          if isyesterday(NSDate()) {
              return "Yesterday"
          }
          if isweek(NSDate()) {
  
              return "a week ago"
          }
  
  
  
  
          return (DateComponents.formatter.stringFromTimeInterval(NSDate().timeIntervalSinceDate(self)) ?? "") + " ago"
      }
      func isyesterday(date: NSDate) -&gt; Bool {
          return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.yesterday)
      }
      var yesterday: NSDate {
          return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: day-1, hour: 0, minute: 0, second: 0, nanosecond: 0)!
      }
  
  
  
      func isweek(date: NSDate) -&gt; Bool {
          return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.weekago)
      }
      var weekago: NSDate {
          return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: day-7, hour: 0, minute: 0, second: 0, nanosecond: 0)!
      }
  
      func isecond(date: NSDate) -&gt; Bool {
          return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.secondsago)
      }
      var secondsago: NSDate {
          return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: 0, hour: 0, minute: 0, second: second-15, nanosecond: 0)!
      }
  
      func ishour(date: NSDate) -&gt; Bool {
          return  NSCalendar.autoupdatingCurrentCalendar().isDate(self, inSameDayAsDate: date.hourago)
      }
      var hourago: NSDate {
          return  NSCalendar.autoupdatingCurrentCalendar().dateWithEra(1, year: year, month: month, day: 0, hour: hour-1, minute: minute-1, second: 0, nanosecond: 0)!
      }
  }
  </code></pre>
  
  <p>here what i got in my app :
  <a href="https://i.stack.imgur.com/8UnOJ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8UnOJ.png" alt="enter image description here"></a></p>
  
  <p>the quotes which is "an hour ago" , " Just now", etc not showing, only " Yesterday" and i'm not sure if it is from 7 days to 23 hours only!</p>
  
  <p>and here is my first question link : <a href="https://stackoverflow.com/questions/34439560/swift-how-to-convert-parse-createdat-time-to-time-ago/34441658?noredirect=1#comment56649948_34441658">Swift How to convert Parse createdAt time to time ago?</a></p>
  
  <p>Please help me to fix the code,
  thanks to @Leo Dabus for the code.</p>