SwiftUI Date picker show only month or only year - datepicker

Is there any possibility to show only month or only year in Picker?
DatePicker(selection: $rkManager.minimumDate, in: Date().defaultDateFrom...Date(), displayedComponents: .date) {
Text("")
}

Related

Combine Date in swiftui to obtain only one Date

Hello I have two date picker one only for date and another for hour and minute
if(showStartDatePicker) {
DatePicker("Seleziona data", selection: $selectedStartDate,/* in: startingDate...endingDate,*/ displayedComponents: [.date])
.datePickerStyle(.graphical)
} else if(showStartTimePicker) {
DatePicker("Select a date", selection: $selectedStartTime, displayedComponents: [.hourAndMinute])
.datePickerStyle(.wheel)
}
I need to obtain a single date that is the merge of the two date picker
So how can I do?
Just share the variable instead of having 2
struct DoubleDatePickerView: View {
#State var showStartDatePicker: Bool = false
#State var selectedStartDate: Date = Date()
var body: some View {
VStack{
Text(selectedStartDate.description)
if(showStartDatePicker) {
DatePicker("Seleziona data", selection: $selectedStartDate,/* in: startingDate...endingDate,*/ displayedComponents: [.date])
.datePickerStyle(.graphical)
Button("show time picker", action: {
showStartDatePicker.toggle()
})
} else {
DatePicker("Select a date", selection: $selectedStartDate, displayedComponents: [.hourAndMinute])
.datePickerStyle(.wheel)
Button("show date picker", action: {
showStartDatePicker.toggle()
})
}
}
}
}
The other option is to combine the desired components from each variable
var combined: Date{
let timeComponents: DateComponents = Calendar.current.dateComponents([.hour,.minute,.second,.timeZone], from: selectedStartTime)
let dateComponents: DateComponents = Calendar.current.dateComponents([.year,.month,.day], from: selectedStartDate)
let combined: DateComponents = .init(calendar: .current, timeZone: timeComponents.timeZone, year: dateComponents.year, month: dateComponents.month, day: dateComponents.day, hour: timeComponents.hour, minute: timeComponents.minute, second: timeComponents.second)
return Calendar.current.date(from: combined) ?? Date()
}

How to set the time to a standard value when using a DatePicker in SwiftUI

I have a date-picker in my app, which only selects the date, not the time. Looks like this:
DatePicker("Birthday",
selection: $date,
displayedComponents: [.date])
A date picker gives you a date object to work with. So, when the user selects a date, it of course also comes with a time, timezone, etc. My problem is, that the time is set to the current time by default. However, I need it to be set to midnight always. How can I provide a default value?
A possible solution that came to my mind was using a formatter and storing the date as a String (instead of a Date), but since I need to do some work with it later (e.g. calculate days between two dates), this doesn't seem like the best approach to me.
you could try this:
struct ContentView: View {
#State var date = Date()
var body: some View {
VStack {
Text("\(date)")
DatePicker("Birthday", selection: $date, displayedComponents: [.date])
.onChange(of: date) { newDate in
if let midnight = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: newDate) {
date = midnight
}
}
}
}
}
or this:
struct ContentView: View {
#State var date = Date()
var body: some View {
VStack {
Text("\(date)")
DatePicker("Birthday", selection: $date, displayedComponents: [.date])
}
.onAppear {
if let midnight = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date()) {
date = midnight
}
}
}
}
or this:
struct ContentView: View {
#State var date = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
var body: some View {
VStack {
Text("\(date)")
DatePicker("Birthday", selection: $date, displayedComponents: [.date])
}
}
}

SwifUI 2 : DatePicker display different format of selected Date

I build a iOS 14 DatePicker. When I selected a date from the picker, it some date in format 27 Dec 2020 and some date show in format 26/12/2020. May I know what is the problem and how can I make it consistent?
struct ContentView : View {
#State private var birthDate = Date()
var body: some View {
VStack(alignment: .leading){
DatePicker(selection: $birthDate, in: Date()..., displayedComponents: .date){
Text("Date")
}
}.padding()
}
}

How can I change DatePicker font weight in SwiftUI

I've tried below code. But .font function does not change anything. I want to the date more darker. How can I do that?
struct TestView: View {
#State private var date = Date()
var body: some View {
HStack {
DatePicker("", selection: $date, in: ...Date(), displayedComponents: .date)
.padding()
.labelsHidden()
.accentColor(.red)
.font(Font.body.weight(.bold))
Spacer()
}
}
}
While you can't specifically change the text for the DatePicker, you can use and HStack to accomplish the desired outcome.
HStack {
Text("End Date")
.font(.custom((Fonts.gse), size: 18))
.fontWeight(.medium)
Spacer()
DatePicker("", selection: self.$endingDate, in: self.dateRange, displayedComponents: [.date, .hourAndMinute])
}

SwiftUI - Date Picker not showing correctly

I´ve created a DatePicker element in SwiftUI, but the DatePicker opens inside the view instead of a sheet at the bottom. (Screenshot below)
My code:
#ObservedObject private var report = Report(
id: 0,
timestamp: Date(timeIntervalSinceReferenceDate: 118800),
date: Date(timeIntervalSinceReferenceDate: 118800),
)
Section(header: Text("Allgemeine Daten")) {
HStack {
Label(string: "Name")
Divider()
TextField("Name", text: $person.name)
}
HStack {
Label(string: "Monat")
Divider()
DatePicker(selection: $report.date, displayedComponents: .date) {
Text("Select a date")
}
}
}
The Section is inside a NavigationView and Form. What could be the issue?
This is because it's actually getting temporarily added to the view hierarchy where your DatePicker is. Since your date picker is in an HStack, it gets appended to the end of the HStack resulting in what you see. If you add the DatePicker by itself, then it gets added to the Section instead, and will shift content below. It's still a pretty terrible animation/user experience, though, so I have to assume it's a bug.