DRN
DRN

Reputation: 155

How can we get all the days in selected month

I'm trying to access all the dates from starting to end of the month with the date and day.

struct DateStore {
    static let shared = DateStore()
    
    func monthDays( for selectedDate: Date) -> [Date] {
        var dates = [Date]()
        // if selectedDate 2020/09/01
        // get dates like  [01 Sep, 02 Sep, 03 Sep, 04 Sep ...30 Sep]
        
        // if selectedDate 2020/08/10
        // get dates like  [01 Aug, 02 Aug, 03 Aug, 04 Aug, ...31 Aug]

        return dates
    }
}

Upvotes: 0

Views: 365

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236370

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]

Upvotes: 6

Related Questions