Kobe Kobe - 2 months ago 9
Swift Question

How to get the current day number in current month and year

I am using this code to get the number of day in the current week:

static func currentDayOfWeek() -> Int {
let comp = NSCalendar.current.dateComponents([.weekday], from: Date())
if comp.weekday == 1 {
return 7
}

return comp.weekday! - 1
}

Monday = 1
Tuesday = 2
...........
Sunday = 7


I can't manage to figure out how to get the current day number of the month and year.

For example for today current day number for the month should be
3
and for the year it should be
277
.

Any suggestions ?

Answer
let dayYear = Calendar.current.ordinality(of: .day, in: .year, for: Date())

let dayMonth = Calendar.current.ordinality(of: .day, in: .month, for: Date())