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

c8ib6hqw  于 2023-03-22  发布在  Swift
关注(0)|答案(1)|浏览(150)

我想使用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

然而,我正在寻找一种更简单更简洁的方法来做到这一点。我想在给定月份的,给定周的,给定天的小时中做同样的事情,我可以做到,但它使用相同的循环。

qojgxg4l

qojgxg4l1#

这就是我所做的-坚持“循环”: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

相关问题