calendar 是用于存储计算时间的结构体,可根据具体使用确定其单位,判断依据等信息。
其中Identifier 定义如下,可知有阳历阴历等计算时间的方法,可根据实际情况选择。
public enum Identifier {
/// The common calendar in Europe, the Western Hemisphere, and elsewhere.
case gregorian
case buddhist
case chinese
case coptic
case ethiopicAmeteMihret
case ethiopicAmeteAlem
case hebrew
case iso8601
case indian
case islamic
case islamicCivil
case japanese
case persian
case republicOfChina
/// A simple tabular Islamic calendar using the astronomical/Thursday epoch of CE 622 July 15
case islamicTabular
/// The Islamic Umm al-Qura calendar used in Saudi Arabia. This is based on astronomical calculation, instead of tabular behavior.
case islamicUmmAlQura
}
components可使用open func components(_ unitFlags: NSCalendar.Unit, from startingDate: Date, to resultDate: Date, options opts: NSCalendar.Options = []) -> DateComponents 方法来获取。其中unit是定义其计算单位,应
与
最后使用单位相同。
另附使用代码如下
let gregorian = Calendar(identifier: Calendar.Identifier.gregorian)
let now = Date()
let components = (gregorian as NSCalendar?)?.components(NSCalendar.Unit.month, from: birthdayPicker.date, to: now, options: [])
let age:Int! = components?.month
print("时间\(age)")