Uidatepicker Select Month and Year

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.

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 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.

How to accept only Month and Year using DatePicker in iOS

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

UIDatePicker, display Full Date with YEAR and time

Yes you can. But you will need to combine both. Use the picker view for the years and set your date picker minimum to the first day of the selected year and the maximum to the last day of that year. You will need also to set your UIDatePicker property [datePickerMode][1] to .dateAndTime

import UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var datePicker: UIDatePicker!
let years = Array(Date().year-3...Date().year+3)
var date = Date()
var lastIndex = 3
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
datePicker.date = Date()
}
@IBAction func dateChanged(_ datePicker: UIDatePicker) {
date = datePicker.date
print(datePicker.date.year)
print(datePicker.date)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
pickerView.selectRow(3, inComponent: 0, animated: true)
setDatePickerMinimumAndMaximumDatesFor(year: years[3])
}
func pickerView(_ pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
return years.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(years[row])
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func setDatePickerMinimumAndMaximumDatesFor(year: Int) {
datePicker.minimumDate = year.firstDayOfTheYear
datePicker.maximumDate = year.lastDayOfTheYear
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
setDatePickerMinimumAndMaximumDatesFor(year: years[row])
if let optionalDate = Calendar.current.date(byAdding: .year, value: (lastIndex > row ? row - lastIndex : -(lastIndex - row)), to: date) {
datePicker.date = optionalDate
}
print(datePicker.date.year)
print(datePicker.date)
}
}
extension Date {
var year: Int {
return Calendar.current.component(.year, from: self)
}
}
extension Int {
var firstDayOfTheYear: Date {
return DateComponents(calendar: .current, year: self, month: 1, day: 1).date!
}
var lastDayOfTheYear: Date {
return DateComponents(calendar: .current, year: self, month: 12, day: 31).date!
}
}

Swift Month year pickerView with last 12 months

This is a simple example to get the last 12 months

var last12Months = [Date]()
let firstDayComponent = DateComponents(day: 1)
Calendar.current.enumerateDates(startingAfter: Date(),
matching: firstDayComponent,
matchingPolicy: .nextTime,
direction: .backward,
using: { (date, idx, stop) in
if let date = date {last12Months.append(date) }
if last12Months.count == 12 { stop = true }
})

print(last12Months)
let formatter = DateFormatter()
formatter.dateFormat = "MMMM yyyy"
print(last12Months.map({formatter.string(from: $0)}))


Related Topics



Leave a reply



Submit