I'm making a basic iOS app and I need to return just the current day of the week as a string variable.
Asked
Active
Viewed 468 times
3 Answers
1
Assuming you need the name of the current weekday in the current locale:
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
let today = NSDate()
let calendar = NSCalendar.currentCalendar()
let todayComponents = calendar.components(.CalendarUnitWeekday, fromDate: today)
let weekDayIdx = todayComponents.weekday
let weekday = dateFormatter.weekdaySymbols[weekDayIdx] as! String
println(weekday) // "Wednesday"

Gabriele Petronella
- 106,943
- 21
- 217
- 235
0
Here you go buddy:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
let hour = components.hour
let minutes = components.minute
let month = components.month
let year = components.year
let day = components.day

adamitude
- 87
- 5
0
Welcome to Stack Overflow.
This is not a "write my code for me" site. It's a site for help with specific problems with your code that you have written.
I'll give you some suggestions:
The easiest way to do this is probably with an NSDateFormatter. Take a look at this thread (specifically the answer by Jon Shier)
Objective C - How can i get the weekday from NSDate?
It looks like you want an NSDateFormatter with the format string "EEEE":
let now = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "EEEE"
let weekday = formatter.stringFromDate(now)