Skip to content

Instantly share code, notes, and snippets.

@vendruscolo
Created January 31, 2021 18:18
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 vendruscolo/f426f0c02b28fac9660d31230881af38 to your computer and use it in GitHub Desktop.
Save vendruscolo/f426f0c02b28fac9660d31230881af38 to your computer and use it in GitHub Desktop.
import Foundation
// https://twitter.com/peres/status/1353810351264034818
func daysBetweenDates(start: Date, end: Date, calendar: Calendar) -> [Date] {
guard let diff = cal.dateComponents([.day], from: start, to: end).day else {
return []
}
// if you need to include end in the array, use 0...diff+1
return (0...diff).compactMap {
cal.date(byAdding: .day, value: $0, to: start)
}
}
let cal = Calendar.autoupdatingCurrent
let today = Date()
let marchThird = cal.date(from: DateComponents(calendar: cal, year: 2021, month: 3, day: 3, hour: 12, minute: 0, second: 0))
daysBetweenDates(start: today, end: marchThird!, calendar: cal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment