How do you convert a DateComponents to a Date Object. Currently, I have following code
Calendar.date(from: DateComponents(year: 2018, month: 1, day: 15))
but I'm getting an error stating "No 'date' candidates produce the expected contextual result type 'Date'
Sorry for the basic question, but I'm still struggling with how to work with dates in Swift
You cannot call date(from
on the type. You have to use an instance of the calendar, either the current
calendar
Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 15))
or a fixed one
let calendar = Calendar(identifier: .gregorian)
calendar.date(from: DateComponents(year: 2018, month: 1, day: 15))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With