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