How to set a specific default time for a date picker in Swift
Are you looking for setting the time through a string. If that''s the case you can use a date formatter like this.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date = dateFormatter.dateFromString("17:00")
datePicker.date = date
Adding an init to the Picker class
init(time:String) {
super.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size.height))
setupView()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
if let date = dateFormatter.dateFromString("17:00") {
datePicker.date = date
}
}
Swift set default date to 10 years back
You can get 10 years back date using these lines of code.
let calendar = Calendar.current
let backDate = calendar.date(byAdding: .year, value: -10, to: Date())
print("\(backDate)")
-10
represent the number of years.
How to set the time to a standard value when using a DatePicker in SwiftUI
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])
}
}
}
How to change UIDatePicker to a specific time (in code)
You've to change the time, you can do it using NSDateComponents
and set the modified date to your DatePicker
var calendar:NSCalendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit, fromDate: NSDate())
components.hour = 5
components.minute = 50
datePicker.setDate(calendar.dateFromComponents(components)!, animated: true)
Set default initial time in UIDatePicker ios
What you need is a today date without time and then set the time manually on it to get a date you need. Date components are perfect for that:
func getTodayDate(at: (hour: Int, minute: Int)) -> Date {
let dateComponents = Calendar.autoupdatingCurrent.dateComponents([.year, .month, .day], from: Date())
dateComponents.hour = at.hour
dateComponents.minute = at.minute
return Calendar.autoupdatingCurrent.date(from: dateComponents)
}
Objective-C
- (NSDate *)getTodayDateAt:(NSInteger)hour minute:(NSInteger)minute {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
components.hour = hour;
components.minute = minute;
return [calendar dateFromComponents:components];
}
Now just set it to your date picker:
_datePicker.date = ...
Make sure your date picker uses the same calendar though.
Set default date on DatePicker in SwiftUI?
Why not just like this?
struct ContentView: View {
@State private var birthDate: Date = Calendar.current.date(byAdding: DateComponents(year: -40), to: Date()) ?? Date()
var body: some View {
VStack {
DatePicker(selection: $birthDate, displayedComponents: .date) {
Text("")
}
.padding(10)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
}
}
}
Related Topics
How to Open File Dialog with Swiftui on Platform "Uikit for MAC"
Get Just the Date (No Time) from Uidatepicker
Swift Programming Nserrorpointer Error etc
How to Animate Transition Between Views in Swiftui
What Is the Role of Avcapturedevicetype.Builtindualcamera
Nstextalignment.Justified for Uilabel Does Not Work
How to Get Unmanaged Object from Realm Query in Swift
Nsundomanager Casting Nsundomanagerproxy Crash in Swift Code
How to Benchmark Swift Code Execution
How to Utilize Nslock to Prevent a Function from Firing Twice
Swift Standard Documentation Comment
Strange Error Nw_Protocol_Get_Quic_Image_Block_Invoke Dlopen Libquic Failed
How to Detect a 'Click' Gesture in Swiftui Tvos
Need to Check That Braces in Given Array Are Balanced or Not
Skip Item When Performing Map in Swift
Swiftui Onhover Doesn't Register Mouse Leaving the Element If Mouse Moves Too Fast