0

I am trying to create UIDatePicker in my project which maximum date should be that of yesterday instead of current date. Is there a way to set selected date as yesterday instead of selecting current date?

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79

1 Answers1

1
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var datePicker: UIDatePicker!

    override func viewDidLoad() {
        super.viewDidLoad()

        datePicker.datePickerMode = .Date

        let yesterday = NSCalendar.currentCalendar().dateByAddingUnit(.Day, value: -1, toDate: NSDate(), options: [])!

        datePicker.maximumDate = NSCalendar.currentCalendar().dateBySettingHour(12, minute: 0, second: 0, ofDate: yesterday, options: [])
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571