better way to get the name of the day on ios swift
27,022
Solution 1
Use DateFormatter
Swift 4
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let dayInWeek = dateFormatter.string(from: date)
Swift3
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE" // "EE" to get short style
let dayInWeek = dateFormatter.stringFromDate(date) // "Sunday"
Screenshot
Solution 2
If you want to get the array of day names you could use: weekdaySymbols in Calendar()
example:
let calendar = Calendar(identifier: .gregorian)
let days = calendar.weekdaySymbols
Solution 3
You can check also this DateFormatter
let dayNameFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.locale = .current
dateFormatter.calendar = .current
dateFormatter.dateFormat = "cccc"
return dateFormatter
}()
print(dayNameFormatter.string(from: Date())) Prints today's day name 🤪
c
- stands for day of the week, good answer, official source
Related videos on Youtube
Author by
sarah
Updated on July 09, 2022Comments
-
sarah almost 2 years
i take the name of the day like this
func loadDayName(forDate date: NSDate) -> String{ let myComponents = calendar.components(.Weekday, fromDate: date) let weekDay = myComponents.weekday switch weekDay { case 1: return "Sunday" case 2: return "Monday" case 3: return "Tuesday" case 4: return "Wednesday" case 5: return "Thursday" case 6: return "Friday" case 7: return "Saturday" default: return "Nada" } }
it is working find but it i was wondering if swift comes with some libraries to do that automatically better than me do that in code like manually
-
Darren over 8 yearsYou should use NSDateFormatter with a custom date format. developer.apple.com/library/mac/documentation/Cocoa/Reference/…
-
sarah over 8 years@Darren i already do that but that has nothing to do with the question that is why i didn't show you the code for it
-
Darren over 8 yearsYou can configure a date formatter to print just the full day name for a given NSDate.
-
Leo Dabus over 8 years
-
Joshua Dance about 6 yearsThis is not a duplicate. The other question is more broad. This is the best question for this specific question and answer.
-
Leo almost 6 yearsI agree with Joshua that this is not a duplicate question
-
-
sarah over 8 yearsit is a good option, but the array contains
["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
not full name for days like i want -
Leo over 8 yearsWith
shortWeekdaySymbols
,you can get short String.Like the code I update -
sarah over 8 yearshow did you know that weekdaysSymbols contain the full names? i printed it, both of them contain the short name. +1 for the efforts
-
Leo over 8 yearsThe
weekdaysSymbols
just works well on my playground. You can also use dateFormatter.Like the code I update -
Joshua Dance about 6 yearsIn Swift 4 the stringFromDate is now
formatter.string(from: now)
-
Teodor Ciuraru over 5 yearsYes, but right after you set the
calendar.locale
. -
Iulian Onofrei over 5 yearsMake sure you also set the
locale
for thedateFormatter
if you care about i18n. -
app4g over 2 yearsFrom nsdateformatter.com There isn't an
EE
option.EEEEE
getsMTWTFSS
andEEEEEE
getsMoTuWeThFrSaSu