斯威夫特 3
我已经包括了获取当月的最后一天,以防它对任何人有用。
extension Date {
func lastDayOfMonth() -> Date {
let calendar = Calendar.current
let dayRange = calendar.range(of: .day, in: .month, for: self)
let dayCount = dayRange!.count
var comp = calendar.dateComponents([.year, .month, .day], from: self)
comp.day = dayCount
return calendar.date(from: comp)!
}
func firstDayOfMonth() -> Date {
let calendar: Calendar = Calendar.current
var components: DateComponents = calendar.dateComponents([.year, .month, .day], from: self)
components.setValue(1, for: .day)
return calendar.date(from: components)!
}
}