Setting the Datepicker in iOS to Pick Only the Month and Year

Setting the Datepicker in IOS to pick only the Month and Year

Your question mentions date and year so I it seemed like UIDatePickerModeDate would suffice but as you are looking for month and year which is not an available option. I suggest you consider using a two component UIPickerView object.

Original Answer

You can do this by changing the Mode property under Date Picker to Date in the Attributes Inspector in the right bar ( Cmd + Option + 4 ). You can also do this programmatically,

datePicker.datePickerMode = UIDatePickerModeDate;

How to accept only Month and Year using DatePicker in iOS



yourDatepicker.datePickerMode = UIDatePickerModeDate;


 yourDatepicker.datePickerMode = UIDatePickerMode.Date


yourDatepicker.datePickerMode = .date

the another modes are

  • UIDatePickerModeTime,
  • UIDatePickerModeDate,
  • UIDatePickerModeDateAndTime,
  • UIDatePickerModeCountDownTimer


the above method not working well, then check this link

How do I create a DatePicker in swiftUI that only select years and I want this to be in prespecified range of years?

You can create your custom year picker by using a simple Picker and passing it an array of Int as your years.

struct YearPicker: View {
private let numberFormatter: NumberFormatter = {
let nf = NumberFormatter()
nf.usesGroupingSeparator = false
return nf

@State private var selectedYearIndex: Int
private let years: [Int]

init(start: Int, end: Int) {
let years = Array(start...end)
self._selectedYearIndex = State(initialValue: years.startIndex)
self.years = years

func yearString(at index: Int) -> String {
let selectedYear = years[index]
return numberFormatter.string(for: selectedYear) ?? selectedYear.description

var body: some View {
VStack {
Text("Your birth year is \(yearString(at: selectedYearIndex))")
Picker("Year", selection: $selectedYearIndex) {
ForEach(years.indices) { yearIndex in
Text("\(self.yearString(at: yearIndex))")

struct YearPicker_Previews: PreviewProvider {
static var previews: some View {
YearPicker(start: 2000, end: 2020)


Setting only specific months in UIDatePicker Swift 5

You can try something like this:

datePicker.maximumDate = .month, value: 3, to: Date())

How to display only month and year in UIDatePicker?

You need to make your own custom class.As UIDatePicker not provide any api's to do that.You need to make custom picker and than fill with appropriate datasource.This code will help you and does same.

Setting the Datepicker in IOS to pick only the Day and month

UIDatePicker doesn't support this. You will need to use UIPickerView and add whatever components you need with whatever values should be shown.

To format the date in your text field, use an NSDateFormatter to format the date as needed.

UIDatePicker select Month and Year

Yeah, you probably want to just make your own picker. You don't have to subclass it or anything, though; just use a generic UIPickerView and return appropriate values from your UIPickerViewDelegate/UIPickerViewDataSource methods.

Related Topics

Leave a reply