我有一个datePicker
用于选择开始日期和时间,还有一个durationTextLabel
用于向该日期和时间添加分钟。如果未插入任何文本,我将duration
设置为最小值30,但是
结果日期和时间是相同的。
你看到我误会了吗?
像往常一样非常感谢您。
功能如下:
func setQueryParameters() {
let dateFormatter = DateFormatter()
var convertedDate: String!
dateFormatter.dateFormat = "yyyy/MM/dd/hh/mm"
convertedDate = dateFormatter.string(from: datePicker.date)
let calendar = Calendar.current
let components = (calendar as NSCalendar).components([.year, .month, .day, .weekday, .hour, .minute] , from: datePicker.date)
let year: Int = components.year!
let month: Int = components.month!
let day: Int = components.day!
let weekday: Int = components.weekday!
let hour: Int = components.hour!
let minute: Int = components.minute!
var duration: Double?
duration = Double(durationTextField.text!)
let endDate = datePicker.date.addingTimeInterval(duration!)
let endComponents = (calendar as NSCalendar).components([.hour, .minute], from: endDate)
let endHour: Int = endComponents.hour!
let endMinute: Int = endComponents.minute!
if durationTextField.text != nil {
duration = Double(durationTextField.text!) ?? 30.00
} else { return}
// Opening Time Query parameter
openingTimeQueryStart = Int("\(String(describing: weekday))"+"00"+"00")!
openingTimeQueryEnd = Int("\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))")!
print("opening query is \(openingTimeQueryEnd)")
// Closing Time Query parameter
closingTimeQueryStart = Int("\(String(describing: weekday))"+"\(String(endHour))"+"\(String(endMinute))")!
closingTimeQueryEnd = Int("\(String(describing: weekday))"+"00"+"00")!
print("closing time query is \(closingTimeQueryStart)")
// Booking Query parameter
let bookingQueryString = "\(String(describing: year))"+"\(String(describing: month))"+"\(String(describing: day))"+"\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))"+"\(String(endHour))"+"\(String(endMinute))"
bookingQuery = Int(bookingQueryString)!// ?? openingTimeQuery // found nil unwripping optional
}
答案 0 :(得分:1)
这里有很多问题。
dateFormatter
之外,您从未使用过convertedDate
。因此,删除未使用的代码。duration
处表示的时间应以分钟为单位,但是将其视为秒。您需要乘以60才能将其转换为分钟。openingTimeQueryEnd
之类的所有代码的每个值都取决于两位数,但是您的代码却无法获得理想的结果。例如,一行:
openingTimeQueryEnd = Int("\(String(describing: weekday))"+"\(String(describing: hour))"+"\(String(describing: minute))")!
应改写为:
openingTimeQueryEnd = Int(String(format: "%02d%02d%02d", weekday, hour, minute))!
或作为:
openingTimeQueryEnd = weekday * 10000 + hour * 100 + minute
对其他类似的行进行类似的更改。