I have used the following code to generate dates between two dates. It works in all cases where the day of the month is less than 28.
extension Date {
public func addMonth(n: Int) -> Date {
let calendar = Calendar.current
return calendar.date(byAdding: .month, value: n, to: self)!
}
public func addYear(n: Int) -> Date {
let calendar = Calendar.current
return calendar.date(byAdding: .year, value: n, to: self)!
}
public var monthName: String {
let calendar = Calendar.current
let monthInt = calendar.component(.month, from: self)
return calendar.monthSymbols[monthInt-1]
}
}
var date = Date()
repeat {
date = date.addMonth(n: 1)
print("\(date) \(date.monthName) ")
} while date <= Date().addYear(n: 2)
2020-07-30 13:31:14 +0000 July
2020-08-30 13:31:14 +0000 August
2020-09-30 13:31:14 +0000 September
2020-10-30 14:31:14 +0000 October
2020-11-30 14:31:14 +0000 November
2020-12-30 14:31:14 +0000 December
2021-01-30 14:31:14 +0000 January
2021-02-28 14:31:14 +0000 February
2021-03-28 13:31:14 +0000 March
2021-04-28 13:31:14 +0000 April
2021-05-28 13:31:14 +0000 May
2021-06-28 13:31:14 +0000 June
2021-07-28 13:31:14 +0000 July
2021-08-28 13:31:14 +0000 August
2021-09-28 13:31:14 +0000 September
2021-10-28 13:31:14 +0000 October
2021-11-28 14:31:14 +0000 November
2021-12-28 14:31:14 +0000 December
2022-01-28 14:31:14 +0000 January
2022-02-28 14:31:14 +0000 February
2022-03-28 13:31:14 +0000 March
2022-04-28 13:31:14 +0000 April
2022-05-28 13:31:14 +0000 May
2022-06-28 13:31:14 +0000 June
2022-07-28 13:31:14 +0000 July
I expect the result to be either 28 or 30.
2020-07-30 13:31:14 +0000 July
2020-08-30 13:31:14 +0000 August
2020-09-30 13:31:14 +0000 September
2020-10-30 14:31:14 +0000 October
2020-11-30 14:31:14 +0000 November
2020-12-30 14:31:14 +0000 December
2021-01-30 14:31:14 +0000 January
2021-02-28 14:31:14 +0000 February
2021-03-30 13:31:14 +0000 March
2021-04-30 13:31:14 +0000 April
2021-05-30 13:31:14 +0000 May
2021-06-30 13:31:14 +0000 June
2021-07-30 13:31:14 +0000 July
2021-08-30 13:31:14 +0000 August
2021-09-30 13:31:14 +0000 September
2021-10-30 13:31:14 +0000 October
2021-11-30 14:31:14 +0000 November
2021-12-30 14:31:14 +0000 December
2022-01-30 14:31:14 +0000 January
2022-02-28 14:31:14 +0000 February
2022-03-30 13:31:14 +0000 March
2022-04-30 13:31:14 +0000 April
2022-05-30 13:31:14 +0000 May
2022-06-30 13:31:14 +0000 June
2022-07-30 13:31:14 +0000 July
Does Calendar has built-in functionality to achieve this?
As you observed, adding 1 month n times is not the same as adding n month. Apparently you want the latter, e.g. like this:
let now = Date()
let finalDate = now.addYear(n: 2)
for n in 1... {
let date = now.addMonth(n: n)
print("\(date) \(date.monthName) ")
if date > finalDate { break }
}
Or this:
let now = Date()
let finalDate = now.addYear(n: 2)
let dates = (1...).lazy.map { now.addMonth(n: $0) }
.prefix(while: { $0 <= finalDate } )
for date in dates {
print("\(date) \(date.monthName)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With