Created
December 24, 2014 07:45
-
-
Save icoxfog417/1ce24f4672a72b54dbb0 to your computer and use it in GitHub Desktop.
NSDate Extension(swift)
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
import Foundation | |
extension NSDate { | |
var calendar: NSCalendar { | |
return NSCalendar(identifier: NSGregorianCalendar)! | |
} | |
func after(value: Int, calendarUnit:NSCalendarUnit) -> NSDate{ | |
return calendar.dateByAddingUnit(calendarUnit, value: value, toDate: self, options: NSCalendarOptions(0))! | |
} | |
func minus(date: NSDate) -> NSDateComponents{ | |
return calendar.components(NSCalendarUnit.MinuteCalendarUnit, fromDate: self, toDate: date, options: NSCalendarOptions(0)) | |
} | |
func equalsTo(date: NSDate) -> Bool { | |
return self.compare(date) == NSComparisonResult.OrderedSame | |
} | |
func greaterThan(date: NSDate) -> Bool { | |
return self.compare(date) == NSComparisonResult.OrderedDescending | |
} | |
func lessThan(date: NSDate) -> Bool { | |
return self.compare(date) == NSComparisonResult.OrderedAscending | |
} | |
class func parse(dateString: String, format: String = "yyyy-MM-dd HH:mm:ss") -> NSDate{ | |
var formatter = NSDateFormatter() | |
formatter.dateFormat = format | |
return formatter.dateFromString(dateString)! | |
} | |
func toString(format: String = "yyyy-MM-dd HH:mm:ss") -> String{ | |
var formatter = NSDateFormatter() | |
formatter.dateFormat = format | |
return formatter.stringFromDate(self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment