How do I create an NSDate object with a custom date other than the current date? For example I would like to create a var of yesterday or of 2 days ago.
Code for Swift 2.0
static func yesterDay() -> NSDate {
let today: NSDate = NSDate()
let daysToAdd:Int = -1
// Set up date components
let dateComponents: NSDateComponents = NSDateComponents()
dateComponents.day = daysToAdd
// Create a calendar
let gregorianCalendar: NSCalendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let yesterDayDate: NSDate = gregorianCalendar.dateByAddingComponents(dateComponents, toDate: today, options:NSCalendarOptions(rawValue: 0))!
return yesterDayDate
}