-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatePickerBootcamp.swift
106 lines (89 loc) · 3.38 KB
/
DatePickerBootcamp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// DatePickerBootcamp.swift
// SwiftfulThinkingBootcamp
//
// Created by enesozmus on 12.05.2024.
//
import SwiftUI
struct DatePickerBootcamp: View {
// ... ⬛️
@State private var selectedDate = Date()
var dateFormatter: DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .short
//formatter.timeStyle = .short
return formatter
}
let startingDate: Date = Calendar.current.date(from: DateComponents(year: 2021)) ?? Date()
let endingDate: Date = Calendar.current.date(from: DateComponents(year: 2024)) ?? Date()
// ... 🟨
let dateRange: ClosedRange<Date> = {
let calendar = Calendar.current
let startComponents = DateComponents(year: 2021, month: 12, day: 1)
let endComponents = DateComponents(year: 2021, month: 12, day: 31, hour: 23, minute: 59, second: 59)
return calendar.date(from:startComponents)!
...
calendar.date(from:endComponents)!
}()
// ... ⬛️
var body: some View {
VStack {
// ... 🟦
Text(dateFormatter.string(from: selectedDate))
.font(.title)
// ... 🟦
DatePicker("Select a date", selection: $selectedDate, displayedComponents: [.date, .hourAndMinute]
)
// ... 🟦
DatePicker("Select a date", selection: $selectedDate, in: startingDate...endingDate, displayedComponents: [.date]
)
// ... 🟦
// DatePicker(
// "Start Date",
// selection: $selectedDate,
// displayedComponents: [.date]
// )
// .datePickerStyle(.graphical)
// ... 🟦
DatePicker(
"Start Date",
selection: $selectedDate,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.datePickerStyle(.graphical)
}
}
}
#Preview {
DatePickerBootcamp()
}
/*
🔴 DatePicker
→ A control for selecting an absolute date.
→ Use a DatePicker when you want to provide a view that allows the user to select a calendar date, and optionally a time.
→ You can limit the DatePicker to specific ranges of dates, allowing selections only before or after a certain date, or between two dates.
@State private var date = Date()
let dateRange: ClosedRange<Date> = {
let calendar = Calendar.current
let startComponents = DateComponents(year: 2021, month: 1, day: 1)
let endComponents = DateComponents(year: 2021, month: 12, day: 31, hour: 23, minute: 59, second: 59)
return calendar.date(from:startComponents)!
...
calendar.date(from:endComponents)!
}()
var body: some View {
DatePicker(
"Start Date",
selection: $date,
displayedComponents: [.date]
)
DatePicker(
"Start Date",
selection: $date,
in: dateRange,
displayedComponents: [.date, .hourAndMinute]
)
.datePickerStyle(.graphical)
}
*/