How to take only month and year using DatePicker in iOS

I am creating an application using swift, and this application has one field for accepting the expiration date of a debit card for payment purposes. How can I only accept month and year using DatePicker in Swift.

If this is not possible, use datepicker, and then suggest any other way.

Thanks in advance.

+6
source share
3 answers

Choice-1

Objective-c

yourDatepicker.datePickerMode = UIDatePickerModeDate; 

Swift

  yourDatepicker.datePickerMode = UIDatePickerMode.Date 

Swift3

 yourDatepicker.datePickerMode = .date 

other mods

  • UIDatePickerModeTime,
  • UIDatePickerModeDate,
  • UIDatePickerModeDateAndTime,
  • UIDatePickerModeCountDownTimer

Choice-2

the above method does not work and then check the link

+8
source

This is actually not an answer, but an alternative: why not use the collector for a month, as the collector for a year, as most applications do (also on sites equivalent to popupmenu)

+1
source

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

It saves my time checking the correct dates of the 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 } } 
0
source

All Articles