-2

I would like to create an NSDate. It should be the year 1, the month 1, the day 1, the minute 0, the seccond 0 and the nanoseccond 0. I don not know how I can create this day myself. So do you guys know how to do this in Swift? Thanks a lot for your help!

Vpor
  • 147
  • 1
  • 1
  • 7

2 Answers2

2

That can be done by using NSDateComponents

var dateComponents = NSDateComponents()
dateComponents.second  = 0
dateComponents.minute = 0
dateComponents.hour = 0
dateComponents.day = 1
dateComponents.month = 1
dateComponents.year = 1
dateComponents.nanosecond = 0
let date = NSCalendar.currentCalendar().dateFromComponents(dateComponents)
Zell B.
  • 10,266
  • 3
  • 40
  • 49
2

Use the NSCalendar method designed for this exact purpose:

let myDate = NSCalendar.currentCalendar().dateWithEra(1, year: 1, month: 1, day: 1, hour: 1, minute: 1, second: 1, nanosecond: 0)
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • 1
    I assumed that Patrick was smart enough to look at the arguments and fill in the desired values, instead of blindly copying and pasting code from StackOverflow without even reading it. – Tom Harrington May 20 '15 at 16:55