如何基于日执行功能?

时间:2015-03-16 05:10:38

标签: ios swift nsdate

我有一个如下所示的函数,它根据小时执行一个函数。如何修改它以便在WEEK的那一天执行?

let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(.CalendarUnitHour, fromDate: date)
    let hour = components.hour


    if(hour >= 9 && hour <= 17)
    {
        //set your image 'open image'
        println("Open")
    }
    else{
         //set your image 'close image'
        println("closed")
    }

1 个答案:

答案 0 :(得分:0)

NSCalendarUnit可以使用UInt初始化,因此这为您提供了所有可能的组件:

var flags = NSCalendarUnit(UInt.max)
let components = calendar.components(flags, fromDate: date)
let day=components.day
let hour = components.hour