import Foundation
func getLastDayOfMonth(year: Int, month: Int) -> Int {
let calendar = Calendar.current
var components = DateComponents()
components.year = year
components.month = month + 1
components.day = 0 // 月の翌月の0日目を指定すると、指定した月の最終日が得られる
if let date = calendar.date(from: components) {
let day = calendar.component(.day, from: date)
return day
} else {
return -1 // エラーの場合
}
}
// 使用例
let year = 2024
let month = 2 // 2月
let lastDay = getLastDayOfMonth(year: year, month: month)
print("The last day of \(month)/\(year) is: \(lastDay)")