3

Get the day name (example: Monday, Tuesday, ..... ) from a selected date from datepicker

 let day = dateFormatter.string(from: date)
Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31
SCS
  • 435
  • 3
  • 12

2 Answers2

6

To get the weekday do the following:

let dateFormatter = DateFormatter()
var weekday: String = ""
dateFormatter.dateFormat = "cccc"
weekday = dateFormatter.string(from: date)

Read more about the different formats here: http://userguide.icu-project.org/formatparse/datetime

Hamed
  • 1,678
  • 18
  • 30
PaFi
  • 888
  • 1
  • 9
  • 24
  • not working for me... i want as for date 26/03/2019 as tuesday – SCS Mar 26 '19 at 11:13
  • what result do you get? – PaFi Mar 26 '19 at 12:14
  • getting as Monday but i want as monday – SCS Mar 26 '19 at 12:25
  • just do weekday.lowercased() if you want to have the letter lowercased – PaFi Mar 26 '19 at 12:28
  • yes get, one more thing if i change the date the day will not changing – SCS Mar 26 '19 at 12:38
  • If you dont show your code i cant help you. But all your questions are probably answered somewhere on stackoverflow – PaFi Mar 26 '19 at 16:31
  • func daySelect() { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "EEEE" let vc = CustomDatePickerAlert() vc.view.frame = CGRect(origin: self.view.center, size: CGSize(width: self.view.frame.width - 50, height: vc.view.frame.height)) vc.treatDelegate = self self.present(vc, animated: true) let wday = dateFormatter.string(from: vc.treatmtDatePicker.date) // onClickDate.setTitle(date, for: .normal) print(wday + "") day = wday – SCS Mar 30 '19 at 09:47
  • also a function for select date in dd/mm/yyyy format all two functions call in a button click – SCS Mar 30 '19 at 09:49
4

Add this extension in below any file in the project. (better make an extension file and add there.)

extension Date {
    func dayNameOfWeek() -> String? {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "EEEE"
        return dateFormatter.string(from: self)
    }
}

Usage:

let dayName = date.dayNameOfWeek()
MRizwan33
  • 2,723
  • 6
  • 31
  • 42