Created
July 30, 2019 15:47
-
-
Save ha1f/2152f9e9786bbf8f913c4d1f7a26fa7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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