You can get the range of day in the month, and map the range initializing a new date using the year and month components of the date combined with the day from the range. I am using noon time because not every date starts at 12am:
extension Date {
func datesInSameMonth(using calendar: Calendar = .current) -> [Date] {
let year = calendar.component(.year, from: self)
let month = calendar.component(.month, from: self)
return calendar.range(of: .day, in: .month, for: self)?.compactMap {
DateComponents(calendar: calendar, year: year, month: month, day: $0, hour: 12).date
} ?? []
}
}
print(Date().datesInSameMonth()) // [2020-09-01 15:00:00 +0000, 2020-09-02 15:00:00 +0000, 2020-09-03 15:00:00 +0000, 2020-09-04 15:00:00 +0000, 2020-09-05 15:00:00 +0000, 2020-09-06 15:00:00 +0000, 2020-09-07 15:00:00 +0000, 2020-09-08 15:00:00 +0000, 2020-09-09 15:00:00 +0000, 2020-09-10 15:00:00 +0000, 2020-09-11 15:00:00 +0000, 2020-09-12 15:00:00 +0000, 2020-09-13 15:00:00 +0000, 2020-09-14 15:00:00 +0000, 2020-09-15 15:00:00 +0000, 2020-09-16 15:00:00 +0000, 2020-09-17 15:00:00 +0000, 2020-09-18 15:00:00 +0000, 2020-09-19 15:00:00 +0000, 2020-09-20 15:00:00 +0000, 2020-09-21 15:00:00 +0000, 2020-09-22 15:00:00 +0000, 2020-09-23 15:00:00 +0000, 2020-09-24 15:00:00 +0000, 2020-09-25 15:00:00 +0000, 2020-09-26 15:00:00 +0000, 2020-09-27 15:00:00 +0000, 2020-09-28 15:00:00 +0000, 2020-09-29 15:00:00 +0000, 2020-09-30 15:00:00 +0000]