Get last day of the year using Date int swift?

Viewed 8662

I want to get the las day of the current year, I need this to change with time so if I use this function in 2018 I get as last day 31 of December 2018 but if I use the function right now it should give me 31 December 2017. I know I can get the current date by just using

var current = Date()

And I know that I can get for example this same day one year from now that should be approximately

 var dayComponents = DateComponents()
        dayComponents.day = 365
        let calendar = Calendar(identifier: .gregorian)
        if let lastDate = calendar.date(byAdding: dayComponents, to: Date()) {
            return lastDate
        } else {
            return Date()
        }

The problem is that I need just from now to the end of the year, how can I achieve this?

2 Answers
Related