Skip to content

Instantly share code, notes, and snippets.

@leodabus
Created December 19, 2016 06:38
Show Gist options
  • Save leodabus/616a9a29e1c2fcb8608444678e5a1fad to your computer and use it in GitHub Desktop.
Save leodabus/616a9a29e1c2fcb8608444678e5a1fad to your computer and use it in GitHub Desktop.
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))!
}
}
@leodabus
Copy link
Author

Date().firstDayOfMonth // "Dec 1, 2016, 12:00 AM"
Date().lastDayOfMonth // "Dec 31, 2016, 12:00 AM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment