计算时隙时得到 nil 值 swift
getting a nil value while calculating time slots swift
我正在遵循 post 中的解决方案,但在我的以下函数中 date2
得到了一个 nil 值:
func calculateOpenTimeSlots() {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm"
let weekday = self.actualWeekday
let openingTime = openingTimeArray[weekday!].openingTime
let closingTime = openingTimeArray[weekday!].closingTime
let date1 = formatter.date(from: openingTime)
let date2 = formatter.date(from: closingTime)
var i = 1
timeSlotArray.removeAll()
while true {
let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
let string = formatter.string(from: date!)
if date! >= date2! {break}
i = i + 1
timeSlotArray.append(string)
}
}
数组是:
var openingTimeArray: [(weekday: Int, openingTime: String, closingTime: String)] = [(weekday: 0, openingTime: "10:00", closingTime: "19:00"), (weekday: 1, openingTime: "10:00", closingTime: "19:00"), (weekday: 2, openingTime: "10:00", closingTime: "19:00"), (weekday: 3, openingTime: "10:00", closingTime: "19:00"), (weekday: 4, openingTime: "10:00", closingTime: "19:00"), (weekday: 5, openingTime: "10:00", closingTime: "19:00"), (weekday: 6, openingTime: "10:00", closingTime: "19:00"), (weekday: 7, openingTime: "10:00", closingTime: "19:00")]
openingTime
和 closingTime
值不是零,它们是正确的值,但是在 date1
我得到一个完整的日期,其中小时早 1 小时。
date1
是 2000-01-01 09:00:00 UTC 但我预计它是 10:00.
我仍然不掌握日期格式化程序,我不明白为什么我得到一个零 date2
和一个错误的 date1
。
任何为我指出正确方向的解释都会像往常一样非常有帮助。
非常感谢
我发现问题出在格式声明中 formatter.dateFormat = "hh:mm"
。我将其更改为 formatter.dateFormat = "HH:mm"
,现在可以按预期工作了。
我不得不在 while
循环之前添加一个追加 timeSlotArray.append(openingTime)
因为循环没有追加从开放时间开始的第一个时间段,但我想重写循环以考虑到这一点。关于如何实现这一目标的任何建议?
func calculateOpenTimeSlots() {
timeSlotArray.removeAll()
let formatter = DateFormatter()
// formatter.timeZone = TimeZone(identifier: "UTC")
formatter.dateFormat = "HH:mm"
let weekday = self.actualWeekday
let openingTime = openingTimeArray[weekday!].openingTime
let closingTime = openingTimeArray[weekday!].closingTime
let date1 = formatter.date(from: openingTime)
timeSlotArray.append(openingTime)
let date2 = formatter.date(from: closingTime)
var i = 1
while true {
let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
let string = formatter.string(from: date!)
if date! >= date2! {break}
i = i + 1
timeSlotArray.append(string)
}
print(timeSlotArray)
}
我正在遵循 post date2
得到了一个 nil 值:
func calculateOpenTimeSlots() {
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm"
let weekday = self.actualWeekday
let openingTime = openingTimeArray[weekday!].openingTime
let closingTime = openingTimeArray[weekday!].closingTime
let date1 = formatter.date(from: openingTime)
let date2 = formatter.date(from: closingTime)
var i = 1
timeSlotArray.removeAll()
while true {
let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
let string = formatter.string(from: date!)
if date! >= date2! {break}
i = i + 1
timeSlotArray.append(string)
}
}
数组是:
var openingTimeArray: [(weekday: Int, openingTime: String, closingTime: String)] = [(weekday: 0, openingTime: "10:00", closingTime: "19:00"), (weekday: 1, openingTime: "10:00", closingTime: "19:00"), (weekday: 2, openingTime: "10:00", closingTime: "19:00"), (weekday: 3, openingTime: "10:00", closingTime: "19:00"), (weekday: 4, openingTime: "10:00", closingTime: "19:00"), (weekday: 5, openingTime: "10:00", closingTime: "19:00"), (weekday: 6, openingTime: "10:00", closingTime: "19:00"), (weekday: 7, openingTime: "10:00", closingTime: "19:00")]
openingTime
和 closingTime
值不是零,它们是正确的值,但是在 date1
我得到一个完整的日期,其中小时早 1 小时。
date1
是 2000-01-01 09:00:00 UTC 但我预计它是 10:00.
我仍然不掌握日期格式化程序,我不明白为什么我得到一个零 date2
和一个错误的 date1
。
任何为我指出正确方向的解释都会像往常一样非常有帮助。
非常感谢
我发现问题出在格式声明中 formatter.dateFormat = "hh:mm"
。我将其更改为 formatter.dateFormat = "HH:mm"
,现在可以按预期工作了。
我不得不在 while
循环之前添加一个追加 timeSlotArray.append(openingTime)
因为循环没有追加从开放时间开始的第一个时间段,但我想重写循环以考虑到这一点。关于如何实现这一目标的任何建议?
func calculateOpenTimeSlots() {
timeSlotArray.removeAll()
let formatter = DateFormatter()
// formatter.timeZone = TimeZone(identifier: "UTC")
formatter.dateFormat = "HH:mm"
let weekday = self.actualWeekday
let openingTime = openingTimeArray[weekday!].openingTime
let closingTime = openingTimeArray[weekday!].closingTime
let date1 = formatter.date(from: openingTime)
timeSlotArray.append(openingTime)
let date2 = formatter.date(from: closingTime)
var i = 1
while true {
let date = date1?.addingTimeInterval(TimeInterval(i*30*60))
let string = formatter.string(from: date!)
if date! >= date2! {break}
i = i + 1
timeSlotArray.append(string)
}
print(timeSlotArray)
}