Swift中的NSDateComponents没有返回工作日

时间:2014-11-13 23:15:56

标签: ios swift nsdate nsdatecomponents

import UIKit

let components = NSDateComponents()
components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitDay)
components.setValue(1, forComponent: NSCalendarUnit.CalendarUnitMonth)
components.setValue(2014, forComponent: NSCalendarUnit.CalendarUnitYear)

println(components.weekday) // 9223372036854775807

任何人都有任何想法,为什么我得到9223372036854775807而不是1-7的值?

1 个答案:

答案 0 :(得分:3)

let dateStyler = NSDateFormatter()
dateStyler.dateFormat = "yyyy-MM-dd"

let myDate = dateStyler.dateFromString("2014-01-01")!

let myWeekday = NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitWeekday, fromDate: myDate).weekday