Skip to content

Instantly share code, notes, and snippets.

@vialyx
Created September 17, 2018 16:35
Show Gist options
  • Save vialyx/e7b54cd78e09781e5aad8944334738bd to your computer and use it in GitHub Desktop.
Save vialyx/e7b54cd78e09781e5aad8944334738bd to your computer and use it in GitHub Desktop.
let startOfDay = Calendar.current.startOfDay(for: Date())
let endOfDay = startOfDay + 8 * 60 * 60
let dateInterval = DateInterval(start: startOfDay, end: endOfDay)
let startLunch = startOfDay + 4 * 60 * 60
let lunchInterval = DateInterval(start: startLunch, duration: 60 * 60)
print("intersection: \(String(describing: dateInterval.intersection(with: lunchInterval)))")
// intersection: Optional(2018-09-17 01:00:00 +0000 to 2018-09-17 02:00:00 +0000)
print("intersects: \(dateInterval.intersects(lunchInterval))")
// intersects: true
// Yep! We have a lunch today!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment