Skip to content

Instantly share code, notes, and snippets.

@JeanMeche
Created July 4, 2016 14:16
Show Gist options
  • Save JeanMeche/3f6a71f9d385b90202e72df7ffb25e5f to your computer and use it in GitHub Desktop.
Save JeanMeche/3f6a71f9d385b90202e72df7ffb25e5f to your computer and use it in GitHub Desktop.
NSDate.swift
//
// NSDate.swift
// Smartlink
//
// Created by Matthieu Riegler on 20/11/15.
//
import Foundation
public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs === rhs || lhs.compare(rhs) == .OrderedSame
}
public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedAscending
}
public func >(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == .OrderedDescending
}
extension NSDate: Comparable { }
/* ISO 8601 Format */
public extension NSDate {
public func ISOString() -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
return dateFormatter.stringFromDate(self).stringByAppendingString("Z")
}
public class func dateFromISOString(string: String) -> NSDate {
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return dateFormatter.dateFromString(string)!
}
}
public extension NSDate {
public func startOf(unit:NSCalendarUnit) -> NSDate {
switch unit {
case NSCalendarUnit.Day:
return startOfDay()
case NSCalendarUnit.WeekOfYear:
return startOfWeek()
case NSCalendarUnit.Month:
return startOfMonth()
case NSCalendarUnit.Year:
return startOfYear()
default:
fatalError("Not implemented yet")
}
}
public func endOf(unit:NSCalendarUnit) -> NSDate {
switch unit {
case NSCalendarUnit.Day:
return endOfDay()
case NSCalendarUnit.WeekOfYear:
return endOfWeek()
case NSCalendarUnit.Month:
return endOfMonth()
case NSCalendarUnit.Year:
return endOfYear()
default:
fatalError("Not implemented yet")
}
}
public func startOfDay() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Year, .Month, .Day], fromDate: self)
return calendar.dateFromComponents(comps)!
}
public func endOfDay() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps2 = NSDateComponents()
comps2.day = 1
comps2.second = -1
return calendar.dateByAddingComponents(comps2, toDate: startOfDay(), options: [])!
}
public func startOfWeek() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Year, .Month, .WeekOfYear, .YearForWeekOfYear], fromDate: self)
return calendar.dateFromComponents(comps)!
}
public func endOfWeek() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps2 = NSDateComponents()
comps2.weekOfYear = 1
comps2.second = -1
return calendar.dateByAddingComponents(comps2, toDate: startOfWeek(), options: [])!
}
public func startOfMonth() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Year, .Month], fromDate: self)
return calendar.dateFromComponents(comps)!
}
public func endOfMonth() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps2 = NSDateComponents()
comps2.month = 1
comps2.second = -1
return calendar.dateByAddingComponents(comps2, toDate: startOfMonth(), options: [])!
}
public func startOfYear() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps = calendar.components([.Year], fromDate: self)
return calendar.dateFromComponents(comps)!
}
public func endOfYear() -> NSDate {
let calendar = NSCalendar.currentCalendar()
let comps2 = NSDateComponents()
comps2.year = 1
comps2.second = -1
return calendar.dateByAddingComponents(comps2, toDate: startOfYear(), options: [])!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment