calendar 计算几天后的某天

  • 计算今天后面的5天:
let now = NSDate()
let calendar = NSCalendar.currentCalendar()
let startComponents = calendar.components([NSCalendarUnit.Year,.Month,.Day,.Weekday], fromDate: now)
startComponents.day += 5
let weekendDate = calendar.dateFromComponents(startComponents);
print(weekendDate);
  • NSDate 的extension:
extension NSDate {
    func Year() -> Int {
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.Year, fromDate: self)
        return components.year
    }
    
    func Month() -> Int {
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.Month, fromDate: self)
        return components.month
    }
    
    func Day() -> Int {
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.Day, fromDate: self)
        return components.day
    }
    
    func WeekDay() -> Int {
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(.Weekday, fromDate: self)
        return components.weekday
    }
    
    func first() -> Int {
        let calendar = NSCalendar.currentCalendar()
        let first = calendar.firstWeekday
        return first
    }
}

github 项目地址

你可能感兴趣的:(calendar 计算几天后的某天)