NSDateComponents in Swift is not returning Weekday

2.4k views Asked by At
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

Anyone have any ideas as to why I'm getting 9223372036854775807 instead of a value from 1-7?

1

There are 1 answers

0
Leo Dabus On BEST ANSWER
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