0

I wants to display dates/days from Monday to Friday excluding Saturday/Sunday as mentioned in the image below.

enter image description here

My Scenario:

  1. When screen loads the current week should display.
  2. On previous button click (DisplayedWeek - 1)
  3. On Next button click (DisplayedWeek + 1).

My Work,

After some research this is what i have tried.

        let calendar = Calendar.current

        let currentDate = calendar.startOfDay(for: Date())

        /* value =  0 for current,
           value =  1 for next,
           value = -1 for previous week.

        */
        let nextWeek:Date =  calendar.date(byAdding: .weekOfMonth, value: 1, to: currentDate)! as Date

        let dayOfWeek = calendar.component(.weekday, from: nextWeek)
        let weekdays = calendar.range(of: .weekday, in: .weekOfYear, for: nextWeek)!

        let days = (weekdays.lowerBound ..< weekdays.upperBound).flatMap {

            calendar.date(byAdding: .day, value: $0 - dayOfWeek, to: nextWeek)

        }.filter { !calendar.isDateInWeekend($0) }

        // Week Days from monday to friday
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"

        for date in days {
            print(formatter.string(from: date))
        }

In my case if the day is Saturday or Sunday it should display next week but it is displaying the same week.

Maddy
  • 1,660
  • 11
  • 24

1 Answers1

4

You can get the current week monday's date, and just add n days to it:

Swift 4.1 • Xcode 9.3 or later

extension Calendar {
    static let iso8601 = Calendar(identifier: .iso8601)
}
extension Date {
    var cureentWeekMonday: Date {
        return Calendar.iso8601.date(from: Calendar.iso8601.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self))!
    }
    var currentWeekdays: [Date] {
        return (0...4).compactMap{ Calendar.iso8601.date(byAdding: DateComponents(day: $0), to: cureentWeekMonday) } // for Swift versions earlier than 4.1 use flatMap instead
    }
}

let currentWeekdays = Date().currentWeekdays
print(currentWeekdays)  // ["Jul 17, 2017, 12:00 AM", "Jul 18, 2017, 12:00 AM", "Jul 19, 2017, 12:00 AM", "Jul 20, 2017, 12:00 AM", "Jul 21, 2017, 12:00 AM"]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • how to display previous/next weeks? – Maddy Jul 21 '17 at 18:48
  • When the current day is Saturday or Sunday it display the same week whereas i want to display the next week. – Maddy Jul 21 '17 at 18:49
  • Just add or subtract one week to the current week days array elements – Leo Dabus Jul 21 '17 at 18:50
  • You should add all that extra info to the question. just add a conditional if weekday > friday you get the next week. – Leo Dabus Jul 21 '17 at 18:52
  • Official time in my place is 12:06A.M. your function is also returning Sunday i.e. `2017-07-16`, complete output: `[2017-07-16 19:00:00 +0000, 2017-07-17 19:00:00 +0000, 2017-07-18 19:00:00 +0000, 2017-07-19 19:00:00 +0000, 2017-07-20 19:00:00 +0000]` – Maddy Jul 21 '17 at 19:08
  • don't mess with timezone. Use it as it is. To display your date at local time use DateFormatter – Leo Dabus Jul 21 '17 at 19:09
  • 1
    ok Got it Thanks sir. using `let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" for date in currentWeekdays { print(formatter.string(from: date)) }` resolved my problem – Maddy Jul 21 '17 at 19:24