swift - Check if date falls between 2 dates -
i have code convert string date object
let date2 = keysdata[indexpath.row]["starttime"] as? string let dateformatter = nsdateformatter() dateformatter.dateformat = "yyyy-mm-dd't'hh:mm:ss.sssz" if let date = dateformatter.datefromstring(date2!) { println(date) }
i know if current date falls between 2 days got in array startdate
, endate
you have code conversion of date string in keysdata
nsdate
. assuming have 2 dates in startdate
, enddate
, have check if current date in between:
let startdate = ... let enddate = ... nsdate().isbetweeen(date: startdate, anddate: enddate) extension nsdate { func isbetweeen(date date1: nsdate, anddate date2: nsdate) -> bool { return date1.compare(self) == self.compare(date2) } }
edit: if want perform inclusive range check, use condition:
extension nsdate { func isbetweeen(date date1: nsdate, anddate date2: nsdate) -> bool { return date1.compare(self).rawvalue * self.compare(date2).rawvalue >= 0 } }
Comments
Post a Comment