在开发中我们需要获取某年某个月份中,具体包含了多少天?例如:2020年2月中的天数。实现代码如下:
let dateComponents = DateComponents(year: 2023, month: 2)
let calendar = Calendar.current
let date = calendar.date(from: dateComponents)!
let range = calendar.range(of: .day, in: .month, for: date)!
let numDays = range.count
print(numDays)