Date Picker

Date Picker

by dockui

User-friendly date picker with a 365-day range, pre-selecting the current date for quick and convenient selection. Ideal for seamless date navigation.

Platform
SwiftUI
Code
Native Swift
Format
Component
Access
Free

Code Snippet

struct DateScrollView: View {
    @State private var selectedDate: Date = Date()
    @State private var previousSelectedDate: Date? = nil
    private let calendar = Calendar.current
    var body: some View {
        VStack{
            Text("Choose A Date")
                .foregroundStyle(.primary)
                .font(.system(size: 30)).bold()
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 10) {
                    ForEach(upcomingDates(), id: \.self) { date in
                        DateButton(date: date, selectedDate: $selectedDate, previousSelectedDate: $previousSelectedDate)
                            .onTapGesture {
                                if date != selectedDate {
                                    selectedDate = date
                                }
                            }
                    }
                }
            }
        }
        .padding(2)
    }
    
    func upcomingDates() -> [Date] {
        var dates: [Date] = []
        let currentDate = Date()
        for i in 0..<30 {
            if let date = calendar.date(byAdding: .day, value: i, to: currentDate) {
                dates.append(date)
            }}
        return dates
    }
}

struct DateButton: View {
    let date: Date
    @Binding var selectedDate: Date
    @Binding var previousSelectedDate: Date?
    var body: some View {
        let isSelected = Calendar.current.isDate(date, inSameDayAs: selectedDate)
        let isPreviousSelected = Calendar.current.isDate(date, inSameDayAs: previousSelectedDate ?? Date.distantPast)
        
        Button(action: {
            previousSelectedDate = selectedDate
            selectedDate = date
        }) {
            VStack{
                Text(date.abbreviatedMonth())
                Text(date.dayNumber())
            }
            .padding()
            .background(isSelected && !isPreviousSelected ? Color.blue : Color.gray.opacity(0.2))
            .foregroundColor(isSelected && !isPreviousSelected ? .white : .primary)
            .cornerRadius(10)
            .overlay(
                RoundedRectangle(cornerRadius: 10)
                    .stroke(Color.secondary, lineWidth: (isSelected && !isPreviousSelected) ? 1 : 0))
        }
    }
}

extension Date {
    func abbreviatedMonth() -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "MMM" // Abbreviated month name
        return formatter.string(from: self).uppercased() // Convert to uppercase
    }
    
    func dayNumber() -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "d" // Day of the month
        return formatter.string(from: self)
    }
}

Similar Blogs

View All Articles
How to Translate Xcode String Catalogs with AI

How to Translate Xcode String Catalogs with AI

Learn how to efficiently translate Xcode string catalogs using AI fast, streamlining localization for your iOS and SwiftUI applications.

by dockui
May 4
How to optimize your SwiftUI app in 2026

How to optimize your SwiftUI app in 2026

Discover top strategies to optimize your SwiftUI app in 2026, enhancing performance, responsiveness, and maintainability with modern concurrency.

by dockui
Apr 29
SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI: iOS Confirmation Alert (Confirm + Cancel)

SwiftUI iOS confirmation alert with Confirm and Cancel buttons. Beginners guide to building safe actions, destructive deletes, and commit dialogs.

by dockui
Nov 11
SwiftUI: IOS Haptic Feedback Guide 2026

SwiftUI: IOS Haptic Feedback Guide 2026

Add click to vibrate haptic feedback in SwiftUI. Full guide to UIImpactFeedbackgenerator, success, error, selection, and tap vibration patterns with code.

by dockui
Nov 11

Code copied to clipboard!