title: How to get the closest time to current time from an array of dates ? iOS, Swift 3
body: <p>I am trying to get the closest time (hours and minutes) to current time (hours and minutes) from an array of Date objects.</p>
  
  <p>What I want is not to compare and find the closest date but time (hours and minutes only). Is there any iOS way / Swfit 3 to do that efficiently?</p>
  
  <p><strong>Edit:</strong>
  The closest time is the time by "moving forward" clockwise, not backwards.</p>
  
  <p><strong>Edit2:</strong>
  Here is a sample:</p>
  
  <pre><code>import Foundation
  
  let userCalendar = Calendar.current
  
  var firstDateComponents = DateComponents()
  firstDateComponents.year = 2017
  firstDateComponents.month = 1
  firstDateComponents.day = 1
  firstDateComponents.hour = 23
  firstDateComponents.minute = 50
  let firstDate = userCalendar.date(from: firstDateComponents)
  
  var secondDateComponents = DateComponents()
  secondDateComponents.year = 2016
  secondDateComponents.month = 12
  secondDateComponents.day = 24
  secondDateComponents.hour = 23
  secondDateComponents.minute = 55
  let secondDate = userCalendar.date(from: secondDateComponents)
  
  var thirdDateComponents = DateComponents()
  thirdDateComponents.year = 2017
  thirdDateComponents.month = 3
  thirdDateComponents.day = 1
  thirdDateComponents.hour = 0
  thirdDateComponents.minute = 30
  let thirdDate = userCalendar.date(from: thirdDateComponents)
  
  var fourthDateComponents = DateComponents()
  fourthDateComponents.year = 2017
  fourthDateComponents.month = 3
  fourthDateComponents.day = 1
  fourthDateComponents.hour = 4
  fourthDateComponents.minute = 20
  let fourthDate = userCalendar.date(from: fourthDateComponents)
  
  var currentDateComponents = DateComponents()
  currentDateComponents.year = 2017
  currentDateComponents.month = 3
  currentDateComponents.day = 2
  currentDateComponents.hour = 0
  currentDateComponents.minute = 10
  
  let currentDate = userCalendar.date(from: currentDateComponents)
  let dates = [firstDate, secondDate, thirdDate, fourthDate]
  </code></pre>
  
  <p>The expected result for this example would be a data structure containing (12:30 AM), the third date.</p>
  
  <p><strong>Edit3:</strong>
  Trying to get to the solution:</p>
  
  <p>To get the time components and strip irrelevant Date information:</p>
  
  <pre><code>let dateComponentsFromDates = dates.map { date -&gt; DateComponents in
      let dateComponents = DateComponents(calendar: userCalendar, timeZone: nil, era: nil, year: nil, month: nil, day: nil, hour: userCalendar.component(.hour, from: date!), minute: userCalendar.component(.minute, from: date!), second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)
      return dateComponents
  }
  </code></pre>