Skip to content

Instantly share code, notes, and snippets.

@kashiftriffort
Last active August 7, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashiftriffort/9c6ba9789c2f4d6a748f8a8463d5387c to your computer and use it in GitHub Desktop.
Save kashiftriffort/9c6ba9789c2f4d6a748f8a8463d5387c to your computer and use it in GitHub Desktop.
Compare Date and find hour, minutes and seconds.
//First find a new instance of Date, and get current Calendar details. Now as per our needs we need to find components. In most case we use hour, minute and second. However there are full list of Calendar components like era, year, month, day, hour, minute, second, weekday, weekdayOrdinal, quarter, weekOfMonth, weekOfYear, yearForWeekOfYear, nanosecond, calendar and timeZone
//To get values of hour, minute and second we need to add following code.
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minute = calendar.component(.minute, from: date)
let second = calendar.component(.second, from: date)
print("\(hour):\(minute):\(second)")
//Find difference between two dates using calendar
var compos:Set<Calendar.Component> = Set<Calendar.Component>()
compos.insert(.minute)
let difference = calendar.dateComponents(compos, from: oldDate, to: Date())
if let minutediff = difference.minute {
print(minutediff)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment