Dates & Components in Swift 3.0

The way we work with dates has changed in Swift 3, the change makes it’s way simpler then before. Let’s look at an example:

//CreateDateFromComponents (1st January 2017)

            var newDate = Date()

            let newDateComponents = DateComponents(calendar: Calendar.current, timeZone: nil, era: nil, year: 2017, month: 1, day: 1, hour: nil, minute: nil, second: nil, nanosecond: nil, weekday: nil, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil)

            newDate = Calendar.current.date(from: newDateComponents)!

 

Hope this proves useful. If you would have any suggestions on how to improve you know where to reach me, @MarcMasVi in Twitter.

 

Marc