美文网首页iOS Blog
calendar 计算几天后的某天

calendar 计算几天后的某天

作者: ShawnDu | 来源:发表于2015-12-29 14:53 被阅读339次
    • 计算今天后面的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 计算几天后的某天

        本文链接:https://www.haomeiwen.com/subject/bdhihttx.html