Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jorge Alvarez
9,330 PointsGetting the day of the week IOS
My objective is to display certain information dependent upon what day of the week it is for example Sunday - Friday so really all I need to do is figure out how the computer can let me know on a daily basis what day it is.
override func viewDidLoad() {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute
let dateFormatter = NSDateFormatter()
dateFormatter.timeStyle = .ShortStyle
dateFormatter.dateStyle = .MediumStyle
var stringDate = dateFormatter.stringFromDate(date)
func getDayOfWeek(today:String)->Int? {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
if let todayDate = formatter.dateFromString(today) {
let myCalendar = NSCalendar()
let myComponents = myCalendar.components(.WeekdayCalendarUnit, fromDate: todayDate)
let weekDay = myComponents.weekday
return weekDay
} else {
return nil
}
}
if let weekday = getDayOfWeek("2014-07-27") {
println(weekday)
} else {
println("bad input")
}
super.viewDidLoad()
}
and this just returns zero