var current = Date()
And I know I can get it on the same day one year later, for example, it should be approximate
var dayComponents = DateComponents()< br /> dayComponents.day = 365
let calendar = Calendar(identifier: .gregorian)
if let lastDate = calendar.date(byAdding: dayComponents, to: Date()) {
return lastDate
} else {
return Date()
}
The question is that I need from now to the end of the year. How can I achieve this goal?
// Get the current year
let year = Calendar .current.component(.year, from: Date())
// Get the first day of next year
if let firstOfNextYear = Calendar.current.date(from: DateComponents(year: year + 1 , month: 1, day: 1)) {
// Get the last day of the current year
let lastOfYear = Calendar.current.date(byAdding: .day, value: -1, to: firstOfNextYear)
)
I want to get the current year’s rasday, I need to change over time, so if I use this in 2018 Function, I will get the last day of December 31, 2018, but if I use this function now it should give me December 31, 2017. I know I can get the current date by using
< p>
var current = Date()
And I know I can get it on the same day one year later, for example it should be approximate
var dayComponents = DateComponents()
dayComponents.day = 365
let calendar = Calendar(identifier: .gregorian)
if let lastDate = calendar.date(byAdding: dayComponents, to : Date()) {
r eturn lastDate
} else {
return Date()
}
The question is that I need from now to the end of the year. How can I achieve this goal?
I will get the current year component of the current date. Add one to next year. Then use it to get the first day of the current year. Then subtract 1 day To get the last day of the year.
// Get the current year
let year = Calendar.current.component(.year, from: Date( ))
// Get the first day of next year
if let firstOfNextYear = Calendar.current.date(from: DateComponents(year: year + 1, month: 1, day: 1)) {< br /> // Get the last day of the current year
let lastOfYear = Calendar.current.date(byAdding: .day, value: -1, to: firstOfNextYear)
}
< /p>