获取当前日期/时间和结束日期/时间之间的小时,天,周,月的日期数组

时间:2017-12-11 21:39:13

标签: swift swift3

我想使用Swift 3创建一系列Date对象,并想知道是否有某个API可以简化我想要做的事情,例如:

我想做的其中一件事是在1年(1月到12月)期间制作一系列日期,每个日期代表每个月的第1天。

我知道我可以在循环中做到这一切......

func monthsInYear() {
   var calendar = Calendar.current
   let date = Date()
   let monthsOfYearRange = calendar.range(of: .month, in: .year, for: date)
   print(monthsOfYearRange as Any)

  if let monthsOfYearRange = monthsOfYearRange {
      let year = calendar.component(.year, from: date)

      for monthOfYear in (monthsOfYearRange.lowerBound..<monthsOfYearRange.upperBound) {
         let components = DateComponents(year: year, month: monthOfYear)
         guard let date = Calendar.current.date(from: components) else { continue }
         print(date.description(with: Locale.current))
      }
   }
}

产生以下输出......

Sunday, January 1, 2017 at 12:00:00 AM Eastern Standard Time
Wednesday, February 1, 2017 at 12:00:00 AM Eastern Standard Time
Wednesday, March 1, 2017 at 12:00:00 AM Eastern Standard Time
Saturday, April 1, 2017 at 12:00:00 AM Eastern Daylight Time
Monday, May 1, 2017 at 12:00:00 AM Eastern Daylight Time
Thursday, June 1, 2017 at 12:00:00 AM Eastern Daylight Time
Saturday, July 1, 2017 at 12:00:00 AM Eastern Daylight Time
Tuesday, August 1, 2017 at 12:00:00 AM Eastern Daylight Time
Friday, September 1, 2017 at 12:00:00 AM Eastern Daylight Time
Sunday, October 1, 2017 at 12:00:00 AM Eastern Daylight Time
Wednesday, November 1, 2017 at 12:00:00 AM Eastern Daylight Time
Friday, December 1, 2017 at 12:00:00 AM Eastern Standard Time

然而,我正在寻找的是一种更简洁的方法来做到这一点。我希望在给定月份的,<给定周内的,<某一天内小时执行相同操作,我能够做,但它使用相同的循环。

1 个答案:

答案 0 :(得分:1)

这就是我的用处 - 坚持“循环”:S

func monthsInYear() {
   print()
   print("monthsInYear")

   var calendar = Calendar.current
   let date = Date()
   let monthsOfYearRange = calendar.range(of: .month, in: .year, for: date)
   //print(monthsOfYearRange as Any)

  if let monthsOfYearRange = monthsOfYearRange {
      let year = calendar.component(.year, from: date)

      for monthOfYear in (monthsOfYearRange.lowerBound..<monthsOfYearRange.upperBound) {
         let components = DateComponents(year: year, month: monthOfYear)
         guard let date = Calendar.current.date(from: components) else { continue }
         print(date.description(with: Locale.current))
      }
   }
}

func weeksInMonths() {
   print()
   print("weeksInMonths")

   var calendar = Calendar.current
   let date = Date()
   let weekOfYearRange = calendar.range(of: .weekOfYear, in: .month, for: date)
   //print(weekOfYearRange as Any)

   if let weekOfYearRange = weekOfYearRange {
      let year = calendar.component(.year, from: date)

      for weekOfYear in (weekOfYearRange.lowerBound..<weekOfYearRange.upperBound) {
         let components = DateComponents(weekOfYear: weekOfYear, yearForWeekOfYear: year)
         guard let date = Calendar.current.date(from: components) else { continue }
         print(date.description(with: Locale.current))
      }
   }
}

func daysInWeek() {
   print()
   print("daysInWeek")

   var calendar = Calendar.current
   let date = Date()
   let daysInWeekRange = calendar.range(of: .day, in: .weekOfMonth, for: date)
   //print(daysInWeekRange as Any)

   if let daysInWeekRange = daysInWeekRange {
      let year = calendar.component(.year, from: date)
      let month = calendar.component(.month, from: date)
      let weekOfMonth = calendar.component(.weekOfMonth, from: date)

      for dayOfWeek in (daysInWeekRange.lowerBound..<daysInWeekRange.upperBound) {
         let components = DateComponents(month: month, day: dayOfWeek, weekOfMonth: weekOfMonth)
         guard let date = Calendar.current.date(from: components) else { continue }
         print(date.description(with: Locale.current))
      }
   }
}

func hoursInDay() {
   print()
   print("hoursInDay")

   var calendar = Calendar.current
   let date = Date()
   let hoursInDayRange = calendar.range(of: .hour, in: .day, for: date)
   //print(hoursInDayRange as Any)

   if let hoursInDayRange = hoursInDayRange {
      let year = calendar.component(.year, from: date)
      let month = calendar.component(.month, from: date)
      let weekOfMonth = calendar.component(.weekOfMonth, from: date)
      let day = calendar.component(.day, from: date)

      for hourOfDay in (hoursInDayRange.lowerBound..<hoursInDayRange.upperBound) {
         let components = DateComponents(year: year, month: month, day: day, hour: hourOfDay, weekOfMonth: weekOfMonth)
         guard let date = Calendar.current.date(from: components) else { continue }
         print(date.description(with: Locale.current))
      }
   }
}

运行时产生以下输出:

monthsInYear
Sunday, January 1, 2017 at 12:00:00 AM Eastern Standard Time
Wednesday, February 1, 2017 at 12:00:00 AM Eastern Standard Time
Wednesday, March 1, 2017 at 12:00:00 AM Eastern Standard Time
Saturday, April 1, 2017 at 12:00:00 AM Eastern Daylight Time
Monday, May 1, 2017 at 12:00:00 AM Eastern Daylight Time
Thursday, June 1, 2017 at 12:00:00 AM Eastern Daylight Time
Saturday, July 1, 2017 at 12:00:00 AM Eastern Daylight Time
Tuesday, August 1, 2017 at 12:00:00 AM Eastern Daylight Time
Friday, September 1, 2017 at 12:00:00 AM Eastern Daylight Time
Sunday, October 1, 2017 at 12:00:00 AM Eastern Daylight Time
Wednesday, November 1, 2017 at 12:00:00 AM Eastern Daylight Time
Friday, December 1, 2017 at 12:00:00 AM Eastern Standard Time

weeksInMonths
Sunday, November 26, 2017 at 12:00:00 AM Eastern Standard Time
Sunday, December 3, 2017 at 12:00:00 AM Eastern Standard Time
Sunday, December 10, 2017 at 12:00:00 AM Eastern Standard Time
Sunday, December 17, 2017 at 12:00:00 AM Eastern Standard Time
Sunday, December 24, 2017 at 12:00:00 AM Eastern Standard Time
Sunday, December 31, 2017 at 12:00:00 AM Eastern Standard Time

daysInWeek
Monday, December 10, 1 at 12:00:00 AM GMT-04:56:02
Tuesday, December 11, 1 at 12:00:00 AM GMT-04:56:02
Wednesday, December 12, 1 at 12:00:00 AM GMT-04:56:02
Thursday, December 13, 1 at 12:00:00 AM GMT-04:56:02
Friday, December 14, 1 at 12:00:00 AM GMT-04:56:02
Saturday, December 15, 1 at 12:00:00 AM GMT-04:56:02
Sunday, December 16, 1 at 12:00:00 AM GMT-04:56:02

hoursInDay
Tuesday, December 12, 2017 at 12:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 1:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 2:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 3:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 4:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 5:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 6:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 7:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 8:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 9:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 10:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 11:00:00 AM Eastern Standard Time
Tuesday, December 12, 2017 at 12:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 1:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 2:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 3:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 4:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 5:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 6:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 7:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 8:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 9:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 10:00:00 PM Eastern Standard Time
Tuesday, December 12, 2017 at 11:00:00 PM Eastern Standard Time