Skip to content

Mayur312/DateRangePicker

Repository files navigation

DateRangePicker

A customizable SwiftUI date range picker that extends beyond the capabilities of Apple's built-in DatePicker. It provides a calendar-style interface for selecting both single dates and date ranges with various constraints.

Features

  • Calendar-style date picker with month grid display
  • Support for both single date and date range selection modes
  • Support for date constraints with various range types:
    • ClosedRange<Date> (start...end)
    • PartialRangeFrom<Date> (start...)
  • Automatic date normalization (removes time components)
  • Year selection view for quick navigation
  • Month-to-month navigation
  • Today indicator
  • Customizable accent color
  • Customizable corner radius for date cells
  • Disabled appearance for dates outside allowed range

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/yourusername/DateRangePicker.git", from: "1.0.0")
]

Or add it directly in Xcode:

  1. Go to File > Add Packages...
  2. Enter the repository URL
  3. Click Add Package

Usage

Single Date Selection

@State private var singleDate: Date? = nil

var body: some View {
    DateRangePicker(
        mode: .single,
        startDate: $singleDate,
        endDate: .constant(nil)
    )
}

Date Range Selection

@State private var startDate: Date? = nil
@State private var endDate: Date? = nil

var body: some View {
    DateRangePicker(
        mode: .range,
        startDate: $startDate,
        endDate: $endDate
    )
}

With Date Constraints (From Today Onwards)

@State private var startDate: Date? = nil
@State private var endDate: Date? = nil

var body: some View {
    let today = Calendar.current.startOfDay(for: Date())
    
    DateRangePicker(
        mode: .range,
        startDate: $startDate,
        endDate: $endDate,
        allowedDateRange: today...
    )
}

With Closed Range Constraints (Next 30 Days)

@State private var startDate: Date? = nil
@State private var endDate: Date? = nil

var body: some View {
    let today = Calendar.current.startOfDay(for: Date())
    let thirtyDaysFromNow = Calendar.current.date(byAdding: .day, value: 30, to: today)!
    
    DateRangePicker(
        mode: .range,
        startDate: $startDate,
        endDate: $endDate,
        allowedDateRange: today...thirtyDaysFromNow
    )
}

Custom Color

DateRangePicker(
    color: .purple,
    mode: .range,
    startDate: $startDate,
    endDate: $endDate
)

Custom Corner Radius

// Square corners (0 radius)
DateRangePicker(
    cornerRadius: 0,
    mode: .range,
    startDate: $startDate,
    endDate: $endDate
)

// Default corners (8 radius)
DateRangePicker(
    cornerRadius: 8,
    mode: .range,
    startDate: $startDate,
    endDate: $endDate
)

// Very rounded corners (20 radius)
DateRangePicker(
    cornerRadius: 20,
    mode: .range,
    startDate: $startDate,
    endDate: $endDate
)

Full API

public init(
    color: Color = .primary,
    cornerRadius: CGFloat = 8,
    mode: SelectionMode = .single,
    startDate: Binding<Date?>,
    endDate: Binding<Date?>,
    allowedDateRange: (any RangeExpression<Date>)? = nil,
    normalizeToMidnight: Bool = true
)

Parameters

  • color: The accent color for the picker (default: .primary)
  • cornerRadius: The corner radius for date selection cells (default: 8)
  • mode: Selection mode - .single or .range (default: .single)
  • startDate: Binding to the selected start date
  • endDate: Binding to the selected end date
  • allowedDateRange: Optional range of dates that are selectable
  • normalizeToMidnight: Whether to normalize dates to midnight (removes time components) (default: true)

Example App

Check out the included example app to see all the features in action.

License

[Your License]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages