Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Created December 23, 2014 22:01
Show Gist options
  • Select an option

  • Save natecook1000/4219d013d1214246fdcd to your computer and use it in GitHub Desktop.

Select an option

Save natecook1000/4219d013d1214246fdcd to your computer and use it in GitHub Desktop.
Actually sane ways of getting a particular NSDate
// (c) 2014 Nate Cook, licensed under the MIT license
extension NSDate {
/// Initializes an NSDate instance with the given date and time parameters.
convenience init(year: Int, month: Int, day: Int, hour: Int, minute: Int = 0, second: Int = 0) {
var components = NSDateComponents()
components.year = year
components.month = month
components.day = day
components.hour = hour
components.minute = minute
components.second = second
self.init(timeInterval: 0, sinceDate: NSCalendar.currentCalendar().dateFromComponents(components)!)
}
/// Initializes an NSDate instance with the given date parameters.
convenience init(year: Int, month: Int, day: Int) {
self.init(year: year, month: month, day: day, hour: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment