How to check if a date is in the future with Swift

2 Answers

0 votes
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.

*/

 



answered 5 hours ago by avibootz
0 votes
import Foundation

/// Checks if a date string in "YYYY-MM-DD" format is in the future.
/// Leverages lexicographical sorting inherent to the ISO-8601 standard format.
func isDateStringInFuture(dateString: String) -> Bool? {
    let formatter = ISO8601DateFormatter()
    formatter.formatOptions = [.withFullDate] // Expects "YYYY-MM-DD" format
    
    // 1. Validate and parse target date string
    guard formatter.date(from: dateString) != nil else {
        return nil // Invalid format or date
    }
    
    // 2. Get today's date formatted as "YYYY-MM-DD" in the current time zone
    let todayString = ISO8601DateFormatter.string(from: Date(), timeZone: .current, formatOptions: [.withFullDate])
    
    // 3. Direct lexicographical string comparison
    return dateString > todayString
}

// --- Complete Runnable Program Example ---
let testDateString = "2035-06-15"

print("Checking date: \(testDateString)")

if let isFuture = isDateStringInFuture(dateString: testDateString) {
    if isFuture {
        print("Result: The date is in the FUTURE.")
    } else {
        print("Result: The date is NOT in the future.")
    }
} else {
    print("Error: Invalid date format provided.")
}


/*
run:

Checking date: 2035-06-15
Result: The date is in the FUTURE.

*/

 



answered 5 hours ago by avibootz
...