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?
Asked
Active
Viewed 2,386 times
0
Mohit Kanwar
- 2,785
- 6
- 36
- 56
Thiha Aung
- 4,856
- 8
- 35
- 77
1 Answers
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
- 216,610
- 56
- 458
- 536