将时间添加到日期选择器日期和时间会产生相同的日期和时间。 SWIFT
Adding time to date picker date and time results in same date and time. SWIFT
我有一个 datePicker
用于 select 开始日期和时间,还有一个 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
}
这里有很多问题。
- 除了创建然后从不使用
convertedDate
之外,您实际上从未使用过 dateFormatter
。因此,请删除未使用的代码。
- 您在
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
对其他类似行进行类似更改。
我有一个 datePicker
用于 select 开始日期和时间,还有一个 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
}
这里有很多问题。
- 除了创建然后从不使用
convertedDate
之外,您实际上从未使用过dateFormatter
。因此,请删除未使用的代码。 - 您在
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
对其他类似行进行类似更改。