日历类的使用总结

下面简单了解一下日历的Api:

 public enum Identifier {

        case gregorian        // 公历

        case buddhist        // 佛教日历

        case chinese         // 中国农历

        case coptic         // 亚历山大历

        case ethiopicAmeteMihret        // 埃塞俄比亚日历(公元8世纪左右)

        case ethiopicAmeteAlem        // 埃塞俄比亚日历(公元前5493前后)

        case hebrew                     // 希伯来日历

        case iso8601                // 国际标准ISO

        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
        @available(OSX 10.10, iOS 8.0, *)
        case islamicTabular       // 伊斯兰历法表格,在公元622年7月15日星期四的天文时代使用

        /// The Islamic Umm al-Qura calendar used in Saudi Arabia. This is based on astronomical calculation, instead of tabular behavior.
        @available(OSX 10.10, iOS 8.0, *)
        case islamicUmmAlQura        // 沙特阿拉伯使用的伊斯兰乌姆Qura日历


    }

    /// An enumeration for the various components of a calendar date.
    ///
    /// Several `Calendar` APIs use either a single unit or a set of units as input to a search algorithm.
    ///
    /// - seealso: `DateComponents`
    public enum Component {

        case era        // 时代

        case year        // 年

        case month        // 月

        case day            // 日

        case hour        // 小时

        case minute        // 分钟

        case second        // 秒

        case weekday        // 星期(1为初始值,为周日)

        case weekdayOrdinal    // 日历单位工作日序数(应该是以每月第几周来算)

        case quarter         // 季度(总是为0)

        case weekOfMonth        // 本月第几周

        case weekOfYear        // 本年第几周

        case yearForWeekOfYear    // 周年(如果为中国,则为中国的历史时长:4652)

        case nanosecond    // 纳秒

        case calendar        // 该日历对象

        case timeZone    // 时区对象(可获取当前所在时区,大洲,城市)
    }

获取日历对象的几种方式:

// currentCalendar取得的值会一直保持在cache中,第一次取得以后如果用户修改该系统日历设定,这个值也不会改变。
var c1 = Calendar.current
//如果用autoupdatingCurrentCalendar,那么每次取得的值都会是当前系统设置的日历的值。
let c2 = Calendar.autoupdatingCurrent
//如果想要用公历的时候,就要将NSDateFormatter的日历设置成公历。否则随着用户的系统设置的改变,取得的日期的格式也会不一样。
let c3 = Calendar.init(identifier: Calendar.Identifier.chinese)

日历对象常用的方法:

//设置每周的第一天从星期几开始,比如:1代表星期日开始,2代表星期一开始,以此类推。默认值是1
c1.firstWeekday

//设置每年及每月第一周必须包含的最少天数,比如:设定第一周最少包括3天,则value传入3
c1.minimumDaysInFirstWeek = 3

// 获取日期对应的日历组件(组件中包含年月日等信息)
calendar.dateComponents([.year,.month], from: date)

// 获取最大、最小区间 eg:.day 一个月最多31天,最小28天
c1.minimumRange(of: .day)
c1.maximumRange(of: .day)

// 通过date,获取smaller在larger中的最大范围。
// 比如small为日,larger为月份,那么date如果在2月则会获取到28或者29最大
let range = c1.range(of: .year, in: .month, for: Date.init())

//获取所在日历单位的开始时间及所在日历单位的总秒数
// eg:.month 开始时间就是上个月的最后一天的12:00  interval就是这一个月的时间秒数
c1.dateInterval(of: .month, start: &d!, interval: &t, for: dd)
// 返回date时间的开始时间、结束时间对象
c1.dateInterval(of: .day, for: dd)

// 通过date,获取smaller在larger中是第几位
// 比如small为星期,larger为年,那么会返回smaller在larger中是第几周
c1.ordinality(of: .weekOfMonth, in: .month, for: dd)

// 在date的基础上,增加comps的时间值.即增加多少天或多少秒等...
c1.date(byAdding: DateComponents.init(calendar: nil, timeZone: nil, era: nil, year: nil, month: nil, day: 1, hour: nil, minute: nil, second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil), to: dd)
// 等同效果,只是更便捷
c1.date(byAdding: .day, value: 1, to: dd)
// 返回日历组件,设置对应的时区
c1.dateComponents(in: TimeZone.init(secondsFromGMT: 8 * 60 * 60)!, from: dd)

// 返回日历组件、记录其不同 eg:.day会记录两个时间的天数差
 let comps = c1.dateComponents([.day], from: d!, to: dd)

// 返回date在当天的起始时间
c1.startOfDay(for: dd)

//比较两个时间
c1.compare(d!, to: dd, toGranularity: .month)
// 不同形式的比较
c1.isDate(dd, inSameDayAs: dd)
c1.isDate(dd, equalTo: d!, toGranularity: .hour)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值