How to get all dates between 2 dates in Swift

3 Answers

0 votes
import Foundation

/// Returns all dates between two date strings (inclusive).
///
/// - Parameters:
///   - startDateString: Start date in "yyyy-MM-dd" format.
///   - endDateString: End date in "yyyy-MM-dd" format.
/// - Returns: Array of date strings in "yyyy-MM-dd" format.
func getDatesBetween(
    startDateString: String,
    endDateString: String
) -> [String] {

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    formatter.locale = Locale(identifier: "en_US_POSIX")
    formatter.timeZone = TimeZone(secondsFromGMT: 0)

    guard
        let startDate = formatter.date(from: startDateString),
        let endDate = formatter.date(from: endDateString)
    else {
        return []
    }

    let calendar = Calendar.current

    var dates: [String] = []
    var currentDate = startDate

    // Iterate through all dates (inclusive)
    while currentDate <= endDate {
        dates.append(formatter.string(from: currentDate))

        guard let nextDate = calendar.date(
            byAdding: .day,
            value: 1,
            to: currentDate
        ) else {
            break
        }

        currentDate = nextDate
    }

    return dates
}


let start = "2026-02-26"
let end = "2026-03-02"

let dates = getDatesBetween(
    startDateString: start,
    endDateString: end
)

print(dates)


/*
run:

["2026-02-26", "2026-02-27", "2026-02-28", "2026-03-01", "2026-03-02"]

*/

 



answered 4 days ago by avibootz
0 votes
import Foundation

/// Returns all dates between two dates (inclusive).
///
/// - Parameters:
///   - startDate: The first date in the range.
///   - endDate: The last date in the range.
/// - Returns: An array containing all dates from startDate through endDate.
func datesBetween(startDate: Date, endDate: Date) -> [Date] {
    
    let calendar = Calendar.current
    
    // Normalize dates to the start of the day.
    let start = calendar.startOfDay(for: startDate)
    let end = calendar.startOfDay(for: endDate)
    
    // Return empty array if the range is invalid.
    guard start <= end else {
        return []
    }
    
    var dates: [Date] = []
    var currentDate = start
    
    // Iterate through all dates in the range.
    while currentDate <= end {
        dates.append(currentDate)
        
        guard let nextDate = calendar.date(
            byAdding: .day,
            value: 1,
            to: currentDate
        ) else {
            break
        }
        
        currentDate = nextDate
    }
    
    return dates
}


let calendar = Calendar.current

let start = calendar.date(from: DateComponents(year: 2026, month: 2, day: 26))!

let end = calendar.date(from: DateComponents(year: 2026, month: 3, day: 2))!

let dates = datesBetween(startDate: start, endDate: end)

// Format dates for display.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"

for date in dates {
    print(formatter.string(from: date))
}


/*
run:

2026/02/26
2026/02/27
2026/02/28
2026/03/01
2026/03/02

*/

 



answered 3 days ago by avibootz
0 votes
import Foundation

func datesBetween(startDate: Date, endDate: Date) -> [Date] {
    let calendar = Calendar.current
    let days = calendar.dateComponents([.day], from: startDate, to: endDate).day ?? 0
    
    return (0...days).compactMap {
        calendar.date(byAdding: .day, value: $0, to: startDate)
    }
}

let calendar = Calendar.current

let start = calendar.date(from: DateComponents(year: 2026, month: 2, day: 26))!

let end = calendar.date(from: DateComponents(year: 2026, month: 3, day: 2))!

let dates = datesBetween(startDate: start, endDate: end)

// Format dates for display.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"

for date in dates {
    print(formatter.string(from: date))
}


/*
run:

2026/02/26
2026/02/27
2026/02/28
2026/03/01
2026/03/02

*//

 



answered 3 days ago by avibootz
...