import Foundation
/// Checks if a given year, month, and day are strictly in the future compared to today.
/// - Returns: A boolean indicating whether the date is in the future, or nil if the date is invalid.
func isDateInFuture(year: Int, month: Int, day: Int) -> Bool? {
let calendar = Calendar.current
// 1. Construct date components for the target date
var components = DateComponents()
components.year = year
components.month = month
components.day = day
// 2. Safely create the target Date.
// date(from:) automatically returns nil if the components form an invalid date (e.g., Feb 30).
guard let targetDate = calendar.date(from: components) else {
return nil
}
// 3. Normalize both dates to the start of the day (00:00:00) for a clean day-only comparison
let normalizedTarget = calendar.startOfDay(for: targetDate)
let normalizedToday = calendar.startOfDay(for: Date())
// 4. Compare normalized dates
return normalizedTarget > normalizedToday
}
// --- Complete Runnable Program Example ---
let testYear = 2035
let testMonth = 6
let testDay = 15
print("Checking date: \(testYear)-\(testMonth)-\(testDay)")
if let isFuture = isDateInFuture(year: testYear, month: testMonth, day: testDay) {
if isFuture {
print("Result: The date is in the FUTURE.")
} else {
print("Result: The date is NOT in the future.")
}
} else {
print("Error: Invalid calendar date provided.")
}
/*
run:
Checking date: 2035-6-15
Result: The date is in the FUTURE.
*/