I am getting two date string in this format, "08:00:00" coming from server side as a starting slot or ending slot. I have to get the time of local timezone and then check if the current time lies in between the time interval which is coming from server. But I am unable to do the comparison.
func checkTime() -> Bool {
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "PKT")
let startingSlot = self.selectedArea.startingSlot! //UTC
let endingSlot = self.selectedArea.endingSlot! //UTC
let date = Date().description(with: Locale.current)
let current = date.split(separator: " ")
let currentD:String = String(current[5])
let date1: Date = dateFormatter.date(from: startingSlot)!
let date2: Date = dateFormatter.date(from: endingSlot)!
let currentdate:Date = dateFormatter.date(from: currentD)!
print(date1)
print(date2)
print(current)
if(currentdate >= date1 && currentdate <= date2) {
return true
} else {
return false
}
}
I expect to get true if the current date time lies in between the date1 and date2.