How to Set a Specific Default Time for a Date Picker in Swift

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



Leave a reply



Submit