import Foundation
/**
* Adds a specified number of weeks to the current system date.
*
* - Parameter weeks: The number of weeks to add (supports negative numbers to subtract).
* - Returns: A new `Date` representing the future date, or `nil` if the
calculation overflows or fails.
*/
func addWeeksToCurrentDate(weeks: Int) -> Date? {
// 1. Get the current date and time
let currentDate = Date()
// 2. Use the current calendar to add the specified weeks.
// .weekOfYear cleanly handles calendar-based week increments.
return Calendar.current.date(byAdding: .weekOfYear, value: weeks, to: currentDate)
}
// ==========================================
// Example Usage:
// ==========================================
let weeksToAdd = 4
if let futureDate = addWeeksToCurrentDate(weeks: weeksToAdd) {
// 3. Format the resulting date into standard YYYY-MM-DD format
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let todayString = formatter.string(from: Date())
let futureString = formatter.string(from: futureDate)
print("Today: \(todayString)")
print("After adding \(weeksToAdd) weeks: \(futureString)")
} else {
print("Could not calculate the future date.")
}
/*
run:
Today: 2026-11-08
After adding 4 weeks: 2026-12-06
*/