0

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?

Hemang
  • 26,840
  • 19
  • 119
  • 186
  • Possible duplicate of [Get day of week using NSDate swift](https://stackoverflow.com/questions/25533147/get-day-of-week-using-nsdate-swift) – Rashwan L Jun 07 '17 at 10:00

1 Answers1

2

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)
Phani Sai
  • 1,215
  • 19
  • 33