Skip to content

Instantly share code, notes, and snippets.

@leonid-shevtsov
Created December 21, 2023 21:40
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 leonid-shevtsov/960dcf9af4fd3846be8bb3dec5602da0 to your computer and use it in GitHub Desktop.
Save leonid-shevtsov/960dcf9af4fd3846be8bb3dec5602da0 to your computer and use it in GitHub Desktop.
enum DateIndex {
// All dates are integers counting from this date
static let zeroDate = Calendar.current.date(from: .init(year: 1970, month: 1, day: 1))!
static func fromDate(_ date: Date) -> Int {
Calendar.current.dateComponents([.day], from: zeroDate, to: date).day!
}
static func toDate(_ index: Int) -> Date {
Calendar.current.date(byAdding: .day, value: index, to: zeroDate)!
}
static func today() -> Int {
fromDate(Date())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment