How do I insert a specific date into the Swift3 Calendar?

0

I need to insert a specific date in the Calendar, but I'm only able to add values based on the current date (maybe because when I instantiate, I give a Calendar.current). Does anyone know how to insert (day, month and year) separately?

    
asked by anonymous 17.05.2017 / 16:29

1 answer

0

First you need separate components using DateComponents :

var dateComponents = DateComponents()
dateComponents.year = 2017
dateComponents.month = 5
dateComponents.day = 20

Then you can set a date based on these components:

let calendar = Calendar.current
let date = calendar.date(from: dateComponents)

In addition to year, month and day, there are also other components that can be defined.

    
20.05.2017 / 14:51