You can use dateInterval(of:start:interval:for:)
:
let formatter = DateFormatter()
let calendar: Calendar = .current
let now = Date()
let weekFromNow = calendar.date(byAdding: .weekOfYear, value: 1, to: now)!
var start = weekFromNow
var interval: TimeInterval = 0
guard calendar.dateInterval(of: .weekOfYear, start: &start, interval: &interval, for: weekFromNow) else { return }
let end = calendar.date(byAdding: .day, value: -1, to: start.addingTimeInterval(interval))!
// or let end = start.addingTimeInterval(interval - 1)
// or let end = calendar.date(byAdding: .day, value: 6, to: start)!
print(formatter.string(from: start), "-", formatter.string(from: end))
This will honor the “start of week” on your device.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…