How to enter a specific date in the Swift3 Calendar?

Asked

Viewed 24 times

0

I need to enter a specific date in the Calendar, but I’m only able to add values based on the current date (maybe because when I do, I give a Calendar.Current). Someone knows how to enter (day, month and year) separately?

1 answer

0

First you need the separate components using DateComponents:

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

Hence you can set a date based on these components:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.