Created
November 16, 2014 21:32
-
-
Save gurgeous/812041a3ae4fc952da2d to your computer and use it in GitHub Desktop.
NSDate extensions
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
private var NSDateFormatters = [ String: NSDateFormatter ]() | |
extension NSDate { | |
// | |
// MARK: today | |
// | |
// like NSDate(), but doesn't set any time components so it's always 8am. When | |
// we get dates off the wire they are always set to 8am. Having all our "dates" | |
// with identical times makes comparisons easier. | |
class func today() -> NSDate { | |
let components = Globals.calendar.components(.CalendarUnitYear | .CalendarUnitMonth | .CalendarUnitDay, fromDate: NSDate()) | |
return Globals.calendar.dateFromComponents(components)! | |
} | |
// | |
// MARK: to string | |
// | |
var sun: String { get { return stringFromFormat("EEE") } } | |
var sunday: String { get { return stringFromFormat("EEEE") } } | |
var jan: String { get { return stringFromFormat("MMM") } } | |
var january: String { get { return stringFromFormat("MMMM") } } | |
var monJan1: String { get { return stringFromFormat("EEE MMM d") } } | |
var monJan1st: String { get { return "\(monJan1)\(day.ordinal)" } } | |
// | |
// MARK: yesterday/tomorrow | |
// | |
var yesterday: NSDate { get { return self - 1 } } | |
var tomorrow: NSDate { get { return self + 1 } } | |
// | |
// MARK: components | |
// | |
var year: Int { get { return Globals.calendar.components(.CalendarUnitYear, fromDate: self).year } } | |
var month: Int { get { return Globals.calendar.components(.CalendarUnitMonth, fromDate: self).month } } | |
var day: Int { get { return Globals.calendar.components(.CalendarUnitDay, fromDate: self).day } } | |
var weekday: Int { get { return Globals.calendar.components(.CalendarUnitWeekday, fromDate: self).weekday } } | |
var daysInMonth: Int { | |
get { | |
return Globals.calendar.rangeOfUnit(.CalendarUnitDay, inUnit: .CalendarUnitMonth, forDate: self).length | |
} | |
} | |
// | |
// MARK: month math | |
// | |
func subtractMonths(rhs: NSDate) -> Int { | |
return (year * 12 + month) - (rhs.year * 12 + rhs.month) | |
} | |
// | |
// MARK: with | |
// | |
func with(#day: Int) -> NSDate { | |
let components = Globals.calendar.components(.CalendarUnitYear | .CalendarUnitMonth, fromDate: self) | |
components.day = day | |
return Globals.calendar.dateFromComponents(components)! | |
} | |
func with(#month: Int) -> NSDate { | |
let components = Globals.calendar.components(.CalendarUnitYear | .CalendarUnitDay, fromDate: self) | |
components.month = month | |
return Globals.calendar.dateFromComponents(components)! | |
} | |
func with(#year: Int) -> NSDate { | |
let components = Globals.calendar.components(.CalendarUnitMonth | .CalendarUnitDay, fromDate: self) | |
components.year = year | |
return Globals.calendar.dateFromComponents(components)! | |
} | |
func stringFromFormat(format: String) -> String { | |
var df = NSDateFormatters[format] | |
if df == nil { | |
df = NSDateFormatter(dateFormat: format) | |
NSDateFormatters[format] = df | |
} | |
return df!.stringFromDate(self) | |
} | |
func components(unitFlags: NSCalendarUnit) -> NSDateComponents { | |
return Globals.calendar.components(unitFlags, fromDate: self) | |
} | |
} | |
// | |
// MARK: comparison operators | |
// | |
extension NSDate: Comparable, Equatable { } | |
public func <=(l: NSDate, r: NSDate) -> Bool { return l == r || l < r } | |
public func >=(l: NSDate, r: NSDate) -> Bool { return l == r || l > r } | |
public func <(l: NSDate, r: NSDate) -> Bool { return l.compare(r) == NSComparisonResult.OrderedAscending } | |
public func >(l: NSDate, r: NSDate) -> Bool { return l.compare(r) == NSComparisonResult.OrderedDescending } | |
public func ==(l: NSDate, r: NSDate) -> Bool { | |
return l.isEqualToDate(r) | |
} | |
// | |
// MARK: date/month math | |
// | |
public func +(l: NSDate, r: NSDateComponents) -> NSDate { | |
return Globals.calendar.dateByAddingComponents(r, toDate: l, options: nil)! | |
} | |
public func +(l: NSDate, r: Int) -> NSDate { return l + NSDateComponents(day: r) } | |
public func -(l: NSDate, r: Int) -> NSDate { return l + NSDateComponents(day: -r) } | |
public func >>(l: NSDate, r: Int) -> NSDate { return l + NSDateComponents(month: r) } | |
public func <<(l: NSDate, r: Int) -> NSDate { return l + NSDateComponents(month: -r) } | |
public func +=(inout l: NSDate, r: Int) { l = l + r } | |
public func -=(inout l: NSDate, r: Int) { l = l - r } | |
public func >>=(inout l: NSDate, r: Int) { l = l >> r } | |
public func <<=(inout l: NSDate, r: Int) { l = l << r } | |
public prefix func ++(inout l: NSDate) -> NSDate { | |
l += 1 | |
return l | |
} | |
public postfix func ++(inout l: NSDate) -> NSDate { | |
let old = l | |
l += 1 | |
return old | |
} | |
public func -(l: NSDate, r: NSDate) -> Int { | |
let components = Globals.calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: r, toDate: l, options: nil) | |
return components.day | |
} | |
// | |
// MARK: NSDateComponents | |
// | |
extension NSDateComponents { | |
convenience init(day: Int) { | |
self.init() | |
self.day = day | |
} | |
convenience init(month: Int) { | |
self.init() | |
self.month = month | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment