Kobe Kobe - 1 year ago 153
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
and for the year it should be

Any suggestions ?

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

let dayMonth = Calendar.current.ordinality(of: .day, in: .month, for: Date())
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download