RaffiKian / RKCalendar

SwiftUI Simple Calendar / Date Picker for iOS
MIT License
535 stars 71 forks source link

How do I get data from the calendar ? #19

Closed OmgiKikov closed 4 years ago

OmgiKikov commented 4 years ago

I want to get data from Start and End Date Selection, but I can't figure out how

workingDog commented 4 years ago

try something like this:

import Foundation
import SwiftUI

struct ContentView : View {

@State var isPresented = false

@ObservedObject var rkManager = RKManager(calendar: Calendar.current,
                          minimumDate: Date().addingTimeInterval(-60*60*24*60),
                          maximumDate: Date().addingTimeInterval(60*60*24*365),
                          mode: .dateRange)

var body: some View {
    VStack (spacing: 10) {
        Button(action: { isPresented.toggle() }) {
            VStack {
                Text("Select Range of Dates Selection").foregroundColor(.blue)
            }
        }
        .sheet(isPresented: $isPresented) { RKViewController().environmentObject(rkManager) }
        VStack {
            Text("Start: " + getTextFromDate(rkManager.startDate))
            Text("End: " + getTextFromDate(rkManager.endDate))
        }
    }
}

func getTextFromDate(_ date: Date?) -> String {
    if date == nil { return "" }
    let formatter = DateFormatter()
    formatter.timeZone = TimeZone.current
    formatter.locale = Locale.current
    formatter.dateFormat = "EEEE, MMMM d, yyyy"
    return date == nil ? "" : formatter.string(from: date!)
  }
}

P.S this is using my fork of RKCalendar

OmgiKikov commented 4 years ago

Thank you @workingDog !