在12小时格式(AM到PM,PM到AM)中修复UIDatePicker()的最大和最小时间语言swift

时间:2018-05-22 04:04:42

标签: swift

这是我的代码:

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()

我正在尝试将输入时间startHourendHour传递给名为UIDatePicker()的{​​{1}}。在这里我通过一个输出,它刚刚从11PM开始,但允许我选择10PM。而且我不想把我的输入传递为23,而是将其作为下午11点,6点传递给早上6点。

0 个答案:

没有答案