2

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

 let day = dateFormatter.string(from: date)
Chitra Khatri
  • 1,262
  • 1
  • 14
  • 30
SCS
  • 425
  • 2
  • 12

2 Answers2

5

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,631
  • 17
  • 26
PaFi
  • 819
  • 1
  • 8
  • 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,655
  • 5
  • 29
  • 40