Hi I am trying to check if the current time is within a time range, say 8:00 - 16:30. My code below shows that I can obtain the current time as a string, but I am unsure how I can use this value to check if it is inside the time range specified above. Any help would be greatly appreciated!
var todaysDate:NSDate = NSDate()
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
var dateInFormat:String = dateFormatter.stringFromDate(todaysDate)
println(dateInFormat) // 23:54
You can compare date like this.
You can get year, month, and day from today's date, append those to those date time strings to build new
NSDate
objects. Thencompare
thetodaysDate
to those two resultingNSDate
objects:also, the solution below looks short if i want to see if the time is in a specific range during day
i guess you could modify it, to check for example, if the months are in certain ranges too, e.g:
and continue from there...