0
votes

I have a date picker, and I want to limit the date picker to show the day and date and month, but the month can't be changed, it's attached to the date. i.e. Friday, January 15. January 15 is one component. It should show the current day plus the next 6 days, so one week. I don't want the year.

 let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let currentDate: NSDate = NSDate()
    let components: NSDateComponents = NSDateComponents()
    let minDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
    components.year = +7
    let maxDate: NSDate = gregorian.dateByAddingComponents(components, toDate: currentDate, options: NSCalendarOptions(rawValue: 0))!
     self.datepicker.minimumDate = minDate
    self.datepicker.maximumDate = maxDate

enter image description here

2
You can't use UIDatePicker. You need to use UIPickerView.rmaddy
That's unfortunate, thanks!coolmac

2 Answers

0
votes

First, you have to use two-section UIPickerView and and implement data source for with both month and day sections.

But actually it is not so easy as it seems. At least because you don't know how many days are in February since you don't choose a year.

0
votes

Pardon my ignorance if I'm way off base here, but cannot NSDateFormatter be used here?

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM"
convertedDate = dateFormatter.stringFromDate(currentDate)