Use Date Int Swift to get the last day of the year?

I want to get the Lass day of the year, I need to change over time, so if I use this feature in 2018, I will get December 31, 2018 The last day, but if I use this function now it should give me December 31, 2017. I know I can get the current date by using

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?

I will get the current year component of the current date. Add one to the 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)) {
// 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>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4149 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.