Swift 获取时间之NSDate扩展

前端之家收集整理的这篇文章主要介绍了Swift 获取时间之NSDate扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
extension NSDate{
    func Year() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentYear:Int = curCalendar.component(NSCalendarUnit.Year,fromDate: self)
        return componentYear
    }
    
    //11
    func Month() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentMonth:Int = curCalendar.component(NSCalendarUnit.Month,fromDate: self)
        return componentMonth
    }
    
    //28
    func Day() -> Int {
        let curCalendar:NSCalendar = NSCalendar.currentCalendar()
        let componentDay:Int = curCalendar.component(NSCalendarUnit.Day,fromDate: self)
        return componentDay
    }
}

通过扩展NSDate,可以获取当前年月日时间。

主要思路还是通过NSCalendar获取

原文链接:https://www.f2er.com/swift/322600.html

猜你在找的Swift相关文章