Created
December 19, 2016 06:38
-
-
Save leodabus/616a9a29e1c2fcb8608444678e5a1fad 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
extension Date { | |
var year: Int { return Calendar.autoupdatingCurrent.component(.year, from: self) } | |
var month: Int { return Calendar.autoupdatingCurrent.component(.month, from: self) } | |
var day: Int { return Calendar.autoupdatingCurrent.component(.day, from: self) } | |
var firstDayOfMonth: Date { | |
return Calendar.autoupdatingCurrent.date(from: DateComponents(year: year, month: month, day: 1))! | |
} | |
var lastDayOfMonth: Date { | |
return Calendar.autoupdatingCurrent.date(from: DateComponents(year: year, month: month+1, day: 0))! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Date().firstDayOfMonth // "Dec 1, 2016, 12:00 AM"
Date().lastDayOfMonth // "Dec 31, 2016, 12:00 AM"