Skip to content

Instantly share code, notes, and snippets.

@ha1f
Created July 30, 2019 15:47
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 ha1f/2152f9e9786bbf8f913c4d1f7a26fa7e to your computer and use it in GitHub Desktop.
Save ha1f/2152f9e9786bbf8f913c4d1f7a26fa7e to your computer and use it in GitHub Desktop.
import Foundation
struct YearMonthDay {
var year: Int
var month: Int
var day: Int
/// Extracts yearMonthDay from Date, using calendar.
static func from(_ date: Date, calendar: Calendar = Calendar(identifier: .gregorian)) -> YearMonthDay {
let components = calendar.dateComponents([.year, .month, .day], from: date)
return YearMonthDay(year: components.year!, month: components.month!, day: components.day!)
}
func date(calendar: Calendar = Calendar(identifier: .gregorian)) -> Date {
var components = DateComponents()
components.year = year
components.month = month
components.day = day
return calendar.date(from: components)!
}
}
extension YearMonthDay: Equatable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment