ios calendar 结构体使用

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)")



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值