xxxxxxxxxx
let currentDate = Date()
let nameFormatter = DateFormatter()
nameFormatter.dateFormat = "MMMM" // format January, February, March, ...
let name = nameFormatter.string(from: currentDate)
let index = Calendar.current.component(.month, from: currentDate) // format 1, 2, 3, ...
print(name) // April
print(index) // 4
xxxxxxxxxx
func startOfTheMonth(month: Int, year: Int) -> Date? {
var components = DateComponents()
let calendar = Calendar.current
// Set the components to the first day of the next month
components.year = year
components.month = month
components.day = 1
return calendar.date(from: components)
}
xxxxxxxxxx
let monthAsNr = Calendar.current.component(.month, from: Date())