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.
6 Answers
You should use NSCalendar
for calculating dates. For example, in Swift 3 the date two days before today is:
let calendar = Calendar.current
let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date())
Or in Swift 2:
let calendar = NSCalendar.currentCalendar()
let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: [])
Or to get the first of the month, you can get the day, month and year from the current date, adjust the day to the first of the month, and then create a new date object. In Swift 3:
var components = calendar.dateComponents([.year, .month, .day], from: Date())
components.day = 1
let firstOfMonth = calendar.date(from: components)]
Or in Swift 2:
let components = calendar.components([.Year, .Month, .Day], fromDate: NSDate())
components.day = 1
let firstOfMonth = calendar.dateFromComponents(components)
There are lots of useful functions in the NSCalendar
/Calendar
class, so you should investigate that further. See the NSCalendar class reference for more information.
But I would advise against doing any manual adjustments of date objects by adjusting it by some time interval that is a multiple of the seconds per day (e.g. 24*60*60). That technique works fine if you're just adding some time interval, but for date calculations, you really want to use calendar object, to avoid problems stemming from daylight savings and the like.

- 415,655
- 72
- 787
- 1,044
-
DayCalendarUnit is Deprecated in OS X v10.10. – Leo Dabus Nov 15 '14 at 07:21
-
you should change it to .CalendarUnitDay – Leo Dabus Nov 15 '14 at 07:22
-
let twoDaysAgo = NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: -2, toDate: NSDate(), options: nil) – Leo Dabus Nov 15 '14 at 07:23
-
Can't do nil apparently for options, not anymore at least, swift 2.1. – Nathan McKaskle Nov 10 '15 at 23:03
-
@NathanMcKaskle - Correct, in Swift 2 and latest API, it's `[]` now. And it's just `.Day`, too. Updated answer accordingly. – Rob Nov 10 '15 at 23:09
-
let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: []) it is not worked in ios 7 – kalpesh Apr 21 '16 at 04:25
-
@kalpesh - Correct, `dateByAddingUnit` was introduced in iOS 8. To support iOS 7 you have to use the more cumbersome `dateByAddingComponents`. For example, `let components = NSDateComponents(); components.day = -2; let twoDaysAgo = calendar.dateByAddingComponents(components, toDate: NSDate(), options: [])`. – Rob Apr 21 '16 at 05:21
-
Swift 3.0 would be: `let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: NSDate() as Date)!` – Patrick Sep 03 '16 at 23:58
-
1@Patrick - Or just `calendar.date(byAdding: .day, value: -2, to: Date())`. – Rob Sep 04 '16 at 00:54
This is a solution for Swift 5.1 - XCode 11
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: Date())
Accordingly two days ago:
let twoDaysAgo = Calendar.current.date(byAdding: .day, value: -2, to: Date())

- 751
- 7
- 9
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
}

- 3,210
- 1
- 21
- 30

- 1,508
- 17
- 22
The Rob's answer works great. If you are heavily using that kind of calculations you can even encapsulate that logic and make your own custom extensions and wrappers.
That said i will suggest to take a look too at this fantastic library called SwiftDate. Even if you aren't using it, the README worth reading.
It introduces the definition or Region
that can be super useful for some scenarios and some handy initializers.
Some cool stuffs and samples:
- Math operation with dates:
(1.years - 2.hours + 16.minutes).fromNow()
- Composing time components:
let dateInUTC = (2015.years | 12.months | 25.days | 20.hours | 10.minutes).inUTCRegion
- Classy:
let date = 5.days.fromNow
,let date = 4.hours.ago
- With Regions:
let date = (6.hours + 2.minutes).fromNow(region: inRome)
- And a lot more ...
Hope it helps.

- 12,326
- 11
- 55
- 76
let twoDaysAgo = NSDate(timeIntervalSinceNow: -2*24*60*60)

- 3,938
- 1
- 15
- 18
-
-
1
-
@Andriy, true, as @Rob pointed out in a comment since deleted. Still, it depends on what you need the `NSDate` for. Sometimes something simple like the above will do, and sometimes the `NSCalendar` based solution is not quite right either: "Two days ago" may or may not mean "the exact same clock time on the day before yesterday". – microtherion Nov 15 '14 at 18:50
Please try below code. I think it's simple.
let today = NSDate()
let tomorrow = today.dateByAddingTimeInterval(24 * 60 * 60)
let yesterday = today.dateByAddingTimeInterval(-24 * 60 * 60)
Cheers!!!

- 461
- 1
- 5
- 11