Check that particular date you had contains the Date you give not the time iOS swift

Solution:

The below code is used to compare one date contains one date not the time like if you had 21-03-2019 12:33:00 date with you and you want to check that date contains 22-03-2019 only.The below function easily did that.

call the below function using the method

                if (firstDate.dateisEqualExceptTime(dateCompare: secondDate))!
                {
print("contains")
                } else {
             print("not contains")
       

                }

code:

extension NSDate {
    func dateisEqualExceptTime(dateCompare: NSDate)-> Bool {

        let cal = Calendar.init(identifier: Calendar.current.identifier)
        let originalDate = cal.dateComponents(in: .current, from: dateCompare as Date)
        let Compdate = cal.dateComponents(in: .current, from: self as Date)
        if originalDate.day == Compdate.day && originalDate.month == Compdate.month && originalDate.year == Compdate.year {
            return true
        } else {
            return false
        }
    }
}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift