这是我的代码:
let startHour: Int = 23
let endHour: Int = 6
let date1: NSDate = NSDate()
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!
let components: NSDateComponents = gregorian.components(([.day, .month, .year]), from: date1 as Date) as NSDateComponents
components.hour = startHour
components.minute = 0
components.second = 0
let startDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate
components.hour = endHour
components.minute = 0
components.second = 0
let endDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate
pickers.datePickerMode = .time
pickers.minimumDate = startDate as Date
pickers.maximumDate = endDate as Date
pickers.setDate(startDate as Date, animated: true)
pickers.reloadInputViews()
我正在尝试将输入时间startHour
和endHour
传递给名为UIDatePicker()
的{{1}}。在这里我通过一个输出,它刚刚从11PM开始,但允许我选择10PM。而且我不想把我的输入传递为23,而是将其作为下午11点,6点传递给早上6点。