0

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

coolmac
  • 1,573
  • 2
  • 10
  • 11

2 Answers2

0

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.

Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
  • 1
    @AnkitSrivastava the key words in this solution is `forDate: date`, where date contains the current date with a year as well. But the questioner didn't specified any year. So the problem with February is still actual. – Alexander Perechnev Jan 15 '16 at 06:41
0

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)
Dan O'Leary
  • 916
  • 9
  • 20