title: swift - compare the date today and past
body: <p>I am new of Swift. I have a problem about date.</p>
  
  <p>If I get a date string like '2016-01-25T10:04:53.498Z' but I want to compare date that now and date string. What should I do?</p>
  
  <p>I take TSWeChat as reference.</p>
  
  <pre><code>let currentDate = NSDate() 
  
  func week() -&gt; String {
      let myWeekday: Int = (Calendar.current as NSCalendar).components([NSCalendar.Unit.weekday], from: self).weekday!
      switch myWeekday {
      case 0:
          return "Sun"
      case 1:
          return "Mon"
      case 2:
          return "Tue"
      case 3:
          return "Wed"
      case 4:
          return "Thu"
      case 5:
          return "Fri"
      case 6:
          return "Sat"
      default:
          break
      }
      return "none"
  }
  
  var chatTimeString: String {
      get {
          let calendar = Calendar.current
          let now = Date()
          let unit: NSCalendar.Unit = [
              NSCalendar.Unit.minute,
              NSCalendar.Unit.hour,
              NSCalendar.Unit.day,
              NSCalendar.Unit.month,
              NSCalendar.Unit.year,
          ]
          let nowComponents:DateComponents = (calendar as NSCalendar).components(unit, from: now)
          let targetComponents:DateComponents = (calendar as NSCalendar).components(unit, from: self)
  
          let year = nowComponents.year! - targetComponents.year!
          let month = nowComponents.month! - targetComponents.month!
          let day = nowComponents.day! - targetComponents.day!
  
          if year != 0 {
              return String(format: "%zdY%zdM%zdD %02d:%02d", targetComponents.year!, targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
          } else {
              if (month &gt; 0 || day &gt; 7) {
                  return String(format: "%zdM%zdD %02d:%02d", targetComponents.month!, targetComponents.day!, targetComponents.hour!, targetComponents.minute!)
              } else if (day &gt; 2) {
                  return String(format: "%@ %02d:%02d",self.week(), targetComponents.hour!, targetComponents.minute!)
              } else if (day == 2) {
                  if targetComponents.hour! &lt; 12 {
                      return String(format: "The day before yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else if targetComponents.hour == 12 {
                      return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else {
                      return String(format: "The day before yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                  }
              } else if (day == 1) {
                  if targetComponents.hour! &lt; 12 {
                      return String(format: "Yesterday A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else if targetComponents.hour == 12 {
                      return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else {
                      return String(format: "Yesterday P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                  }
              } else if (day == 0){
                  if targetComponents.hour! &lt; 12 {
                      return String(format: "A.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else if targetComponents.hour == 12 {
                      return String(format: "P.M %02d:%02d",targetComponents.hour!, targetComponents.minute!)
                  } else {
                      return String(format: "P.M %02d:%02d",targetComponents.hour! - 12, targetComponents.minute!)
                  }
              } else {
                  return ""
              }
          }
      }
  }
  </code></pre>