Swift-Date相关

1,NSDate基本知识以及转换成时间字符串

//MARK:- NSDate基本以及转换成时间字符串
  let currentDate = Date.init()
  let timeZone = NSTimeZone.system
  let timeInterval = timeZone.secondsFromGMT(for: currentDate)
  let localDate = currentDate.addingTimeInterval(TimeInterval(timeInterval))

  //1,获取的是时间对象
  print(localDate)

  //2,获取当前时间的字符串
  let dateFormatter = DateFormatter.init()
  dateFormatter.dateFormat = "yyyy年MM月dd日 HH时mm分ss秒 Z"
  let localDateStr = dateFormatter.string(from: localDate)

  print(localDateStr)

2,时间字符串转换成时间对象

//MARK:- 时间字符串转换成时间对象

  let dateStr = "2017年04月02日 19时03分25秒 +0800"

  let dateFormatter = DateFormatter.init()
  dateFormatter.dateFormat = "yyyy年MM月dd日 HH时mm分ss秒 Z"
  let date = dateFormatter.date(from: dateStr)
  print(date ?? "date transform failed")

3,比较时间差值:通过秒数差距进行比较

//MARK:- 比较时间差值:通过秒数差距进行比较

  fileprivate func dateComp(){

      let dateFormatter = DateFormatter.init()
      dateFormatter.dateFormat = "yyyy年MM月dd日 HH时mm分ss秒 Z"

      let firDateStr = "2017年04月02日 19时03分25秒 +0800"
      let firstDate = dateFormatter.date(from: firDateStr)

      let secDateStr = "2017年04月02日 18时03分50秒 +0800"
      let secondDate = dateFormatter.date(from: secDateStr)

      let timeInterval = (firstDate?.timeIntervalSince1970)! - (secondDate?.timeIntervalSince1970)!
      let timeIntervalStr = self.getHHMMSSFormSS(seconds: Int(timeInterval))

      print(timeIntervalStr)
  }

  func getHHMMSSFormSS(seconds:Int) -> String {
      let str_hour = NSString(format: "%02ld", seconds/3600)
      let str_minute = NSString(format: "%02ld", (seconds%3600)/60)
      let str_second = NSString(format: "%02ld", seconds%60)
      let format_time = NSString(format: "相差%@hours, %@minutes, %@seconds",str_hour, str_minute,str_second)
      return format_time as String
  }

4,比较时间差:通过系统自带日历进行比较

//MARK:-  比较时间差:通过系统自带日历进行比较
extension ViewController{

    fileprivate func dateCompByCalendar(){

        let dateFormatter = DateFormatter.init()
        dateFormatter.dateFormat = "yyyy年MM月dd日 HH时mm分ss秒 Z"

        let firDateStr = "2017年04月02日 10时03分25秒 +0800"
        let firstDate = dateFormatter.date(from: firDateStr)

        let secDateStr = "2017年04月12日 18时03分50秒 +0800"
        let secondDate = dateFormatter.date(from: secDateStr)

        let calendar = Calendar.current
        let components = Set([.year, .month, .day, .hour, .minute, .second])

        let comps = calendar.dateComponents(components, from: firstDate!, to: secondDate!)

        print(comps)
        print(firstDate!)
        print(secondDate!)

        print(comps.hour ?? "nil")
    }
}

5,日期比较(日期在前还是在后)

//MARK:- 日期比较

  fileprivate func dateCompBiggerOrSmaller(){

      let dateFormatter = DateFormatter.init()
      dateFormatter.dateFormat = "yyyy年MM月dd日 HH时mm分ss秒 Z"

      //时间01
      let firDateStr = "2017年04月02日 10时03分25秒 +0800"
      let firstDate = dateFormatter.date(from: firDateStr)
      print(firstDate ?? "firstDate is nil")

      //时间02
      let secDateStr = "2017年04月01日 18时03分50秒 +0800"
      let secondDate = dateFormatter.date(from: secDateStr)
      print(secondDate ?? "secondDate is nil")

      //当前时间
      let currentDate = Date.init()
      let timeZone = NSTimeZone.system
      let timeInterval = timeZone.secondsFromGMT(for: currentDate)
      let localDate = currentDate.addingTimeInterval(TimeInterval(timeInterval))

      //1,获取的是时间对象
      print(localDate)

      let comparisonResult = firstDate?.compare(secondDate!)

      if comparisonResult == .orderedAscending{
          print("orderedAscending")
          print("第二个日期大")

      }else if comparisonResult == .orderedDescending{
          print("orderedDescending")
          print("第一个日期大")
      }else{
          print("same")
          print("两个日期一样大")
      }

你可能感兴趣的:(Swift-Date相关)