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?
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?
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.