Hemang
Hemang

Reputation: 27072

How to get the difference of weekdays?

I have this information:

Days  Dec'15  Jan'16
---------------------
Sun    27
Mon    28
Tue    29
Wed    30
Thu    31
Fri             1
Sat             2

I have 1st Jan'16. So I have to get Fri and then the difference of days from the Sun. So, in this case, the difference should be 5. Because, before Friday there are 5 other days. So if I want to know it for 2nd Jan'16 it should be 6. And likewise.

How do I get it easy with date functions?

Upvotes: 0

Views: 103

Answers (1)

Phani Sai
Phani Sai

Reputation: 1233

The following code may help you

extension Date {

    func weekdayDiffence() -> Int {
        return Calendar.current.dateComponents([Calendar.Component.weekday], from: self).weekday ?? 0

    }
}

Example

let d = Date().weekdayDiffence()
print(d)

Upvotes: 2

Related Questions