title: Compare Date to String Date Value Swift
body: <p>I am trying to convert a string date into date type. But when I am trying this code given below return nil value after I compare the two dates with each other, one is selected date from date picker and the second is current date, but always give my return no.</p>
  
  <p>I am really confused how to convert string date to date type  and after compare two dates with each other please check code below:</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>    let currentDate = Date()
      let currentDformatter = DateFormatter()
      currentDformatter.dateFormat = "dd-MM-yyyy"
      let currentDateResult = currentDformatter.date(from: "\(currentDate)")
      print("Current Latest Date ",currentDateResult as Any)
  
      let selectedDate = Date()
      let selectedDformatter = DateFormatter()
      selectedDformatter.dateFormat = "dd-MM-yyyy"
      let selectedresult = selectedDformatter.string(from: selectedDate)
      print("Current Latest Date ",selectedresult)
  
      if currentDateResult == selectedDate{
          print("yes")
      }else{
          print("no")
      }
  </code></pre>
  
  <p>And i am also compare the time please give me advice on how can i prevent the user from selecting time in time picker beyond a certain time. For example like restaurant closing time is 5'o clock.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>CurrentDateAndTime 2019-04-15 05:00:00 +0000
  
  SelectDateAndTime  2019-04-16 07:52:28 +0000
  </code></pre>
  
  <p>I do not want the user to select time after 5'o clock , how can i check and compare the time??</p>
  
  <p>how can i check and compare the time??</p>