How to accept only Month and Year using DatePicker in iOS

11,457

Solution 1

Choice-1

Objective-C

yourDatepicker.datePickerMode = UIDatePickerModeDate;

Swift

 yourDatepicker.datePickerMode = UIDatePickerMode.Date

Swift3

yourDatepicker.datePickerMode = .date

the another modes are

  • UIDatePickerModeTime,
  • UIDatePickerModeDate,
  • UIDatePickerModeDateAndTime,
  • UIDatePickerModeCountDownTimer

Choice-2

the above method not working well, then check this link

Solution 2

I suggest you use this link: https://github.com/bendodson/MonthYearPickerView-Swift

It is saving my time to validate valid dates months

 let expiryDatePicker = MonthYearPickerView()

private func pickADate() {
    self.invitationTypeTemp = self.expiredDetailOutlet.text ?? "unknown error"

    //FORMAT DATE
   //        expiryDatePicker.datePickerMode = .date

    expiryDatePicker.backgroundColor = UIColor.white
    expiryDatePicker.setValue(UIColor.black, forKey: "textColor")
    expiryDatePicker.autoresizingMask = .flexibleWidth
    expiryDatePicker.contentMode = .center
    expiryDatePicker.frame = CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 300)
    self.view.addSubview(expiryDatePicker)

    toolBar = UIToolbar.init(frame: CGRect.init(x: 0.0, y: UIScreen.main.bounds.size.height - 300, width: UIScreen.main.bounds.size.width, height: 50))
    toolBar.barStyle = .default
    toolBar.isTranslucent = true
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(onDoneButtonTappedDateExpired))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(onCancelButtonTappedDateExpired))

    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true
    self.view.addSubview(toolBar)

    //DISABLE RIGHT ITEM & LEFT ITEM
   //        disableCancelAndSaveItems()

    //DISABLED SELECTION FOR ALL CELLS
   //        ableToSelectCellsAndButtons(isAble: false)

    //DISABLE RIGHT ITEM & LEFT ITEM
    isEnableCancelAndSaveItems(isEnabled: false)

    //SHOW GREY BACK GROUND
    showGreyOutView(isShow: true)
    expiryDatePicker.onDateSelected = { (month: Int, year: Int) in
        let string = String(format: "%02d/%d", month, year)
        self.expiredDetailOutlet.text = string
        NSLog(string) // should show something like 05/2015
    }
}

Solution 3

Not really an answer but an alternative: why not use a picker for month an a picker for year like most apps do (also on websites the equivalent a popupmenu is used)

Share:
11,457
Chetan Purohit
Author by

Chetan Purohit

I am a NodeJS REST API developer at Veloziti Inc. since last 2 years.

Updated on July 25, 2022

Comments

  • Chetan Purohit
    Chetan Purohit almost 2 years

    I am building an app using swift and in that app there is one field for accepting expiry date for Debit card for payment purpose. How can I accept only month and year by using DatePicker in Swift.

    If it is not possible using datepicker then please suggest any another way.

    Thank you in advance.

  • Chetan Purohit
    Chetan Purohit almost 9 years
    Thanks for your suggestion. :-)
  • Chetan Purohit
    Chetan Purohit almost 9 years
    Thanks for your reply. :-)
  • Anbu.Karthik
    Anbu.Karthik almost 9 years
    @ChetanPurohit --if the first is not hopeful try the second choice , it is very useful for you
  • Chetan Purohit
    Chetan Purohit almost 9 years
    Thanks for your advice Karthik. I will try it.
  • Chetan Purohit
    Chetan Purohit almost 9 years
    Thank you very much dude. That is what I was looking for. i will use the second one.
  • Anbu.Karthik
    Anbu.Karthik almost 9 years
    @ChetanPurohit -- if my answer is fine give the upvote for the answer, it is useful for future
  • Ben Patch
    Ben Patch about 8 years
    This may work for smaller apps, but for apps working in several countries it would be a bad option because of the different calendars countries use.