-3

I want to check today's date with some other date which is in String, if both are equals then need to perform some tasks.

I tried date1 < date2 but it is comparing date, I want equal check

  • Please do not use date formatters for comparing dates. The selected answer is wrong (as are most of the others). Date formatters should only be used for displaying dates to the user. I have added an answer that uses a built in `Calendar` function to do this for you. – Fogmeister Apr 17 '19 at 11:12
  • I want compare String which has date `eg. 10.10.2010` with today's date which is in date format so I used above code – Appstute Apr 17 '19 at 11:19
  • 1
    In that case you should use a date formatted to turn the string into a Date. And then use the calendar function to compare it with another Date. Don’t compare dates as strings. – Fogmeister Apr 17 '19 at 11:21
  • Where does the string come from that you are using? How are you getting that string? – Fogmeister Apr 17 '19 at 12:26

3 Answers3

1

Dates are complicated... let Apple do it for you... Calendar date comparison

let date1 = // some date
let date2 = // some date

let isSameDay = Calendar.current.isDate(date1, equalTo: date2, toGranularity: .day)
let isSameHour = Calendar.current.isDate(date1, equalTo: date2, toGranularity: .hour)
let isSameMonth = Calendar.current.isDate(date1, equalTo: date2, toGranularity: .month)

etc...

This will work no matter which time zone, which calendar type is being used, etc...

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
-2

compare both date string like,

if date1.stringWithFormatter(dateFormat: "yyyy-MM-dd") == Date().stringWithFormatter(dateFormat: "yyyy-MM-dd") {
// today's date
}else{
// not today's date
}

extension Date {

    func stringWithFormatter(dateFormat: String) -> String {
        let formatter: DateFormatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = dateFormat
        return formatter.string(from: self)
    }
    func datefromString(string:String,dateFormat: String) -> Date {
        let formatter: DateFormatter = DateFormatter()
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = dateFormat
        return formatter.date(from: string)!
    }

}
  • 2
    DateFormatters should only be used for displaying a human readable date to the user. Not for comparing dates. – Fogmeister Apr 17 '19 at 11:13
  • but logically you can get result through this code. i think if you get always right answer from any code then code is correct. – Proxion Group Apr 17 '19 at 11:56
  • 2
    There are many reasons why this code is not correct. One of which is that it’s using date formatted strings to compare dates. – Fogmeister Apr 17 '19 at 12:24
-3

Use below code to check equal dates

func getCurrentDate() -> String {

    let date = Date()
    let formatter = DateFormatter()
    formatter.dateFormat = "dd.MM.yyyy"
    let result = formatter.string(from: date)

    print(result)
    return result
}

Now check with your date

if self.getCurrentDate() == "01.01.2001" {
     \\Your stuff to be added
      print("Date found!!!")
}
Sayali Shinde
  • 311
  • 2
  • 6
  • DateFormatters should only be used for displaying a human readable date to the user. Not for comparing dates. Please do not use this method. – Fogmeister Apr 17 '19 at 11:13
  • According to Appstute, has to compare string which has date with today's date which is in date format so I used above code. – Sayali Shinde Apr 17 '19 at 11:17
  • 1
    In this case they should use a date formatted to turn the string into a date and then use Calendar to compare it with another date. Never work with dates as strings. – Fogmeister Apr 17 '19 at 11:22
  • 1
    `Date()` has `UTC` timezone means when it is `01.01.2001` in your country, it may or may not be the same date in `UTC timeZone`. You should not ignore this and use the `Calendar`'s helper methods as mentioned in @Fogmeister answer. – Kamran Apr 17 '19 at 11:26