2
我看到DateComponents has an Instance Property isLeapMonth。它似乎是一个重要的财产。我真正想知道的是一年,一个月是一个闰月。这可能在API中,还是我需要实现我自己的算法来做到这一点?提前谢谢了。有没有办法确定一年是否有Swift中的闰月?
我看到DateComponents has an Instance Property isLeapMonth。它似乎是一个重要的财产。我真正想知道的是一年,一个月是一个闰月。这可能在API中,还是我需要实现我自己的算法来做到这一点?提前谢谢了。有没有办法确定一年是否有Swift中的闰月?
您可以检查,如果有问题该月的第一天,当设置为飞跃,是一个有效的日期或不:
func isLeap(month: Int, year: Int, era: Int, calendar: Calendar) -> Bool {
var components = DateComponents()
components.era = era
components.year = year
components.month = month
components.day = 1
components.isLeapMonth = true
return components.isValidDate(in: calendar)
}
// The Chinese year that begins in 2017
isLeap(month: 5, year: 34, era: 78, calendar: Calendar(identifier: .chinese)) // false
isLeap(month: 6, year: 34, era: 78, calendar: Calendar(identifier: .chinese)) // true
// The Chinese year that begins in 2020
isLeap(month: 3, year: 37, era: 78, calendar: Calendar(identifier: .chinese)) // false
isLeap(month: 4, year: 37, era: 78, calendar: Calendar(identifier: .chinese)) // true
据this list,有使用闰月几日历系统作为日期更正机制。中国的日历是我更熟悉的。您可以将其与list of leap months in the Chinese calendar