How to Change Uidatepicker to a Specific Time (In Code)

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)

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
}
}

Restricting enabled time to a specific time span in UIDatePicker swift 4

Restricting user so she/he can only pick date between 9 and 12 but will show all the available options as this is date picker view's default behaviour but by setting proper minimumDate and maximumDate user can only pick between 9 and 21. Try below code and let me know if it works

And if you want to display times of your choice I will suggest better create an array of times you want to display, So user will only choose from the array you will provide In this case times between 9 am and 9 pm like [9:00,9:30,10:00,10:30,.....,21:00] and set that array to normal picker

datePicker.datePickerMode = .time // setting mode to timer so user can only pick time as you want 
datePicker.minuteInterval = 30 // with interval of 30
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"

let min = dateFormatter.date(from: "9:00") //createing min time
let max = dateFormatter.date(from: "21:00") //creating max time
datePicker.minimumDate = min //setting min time to picker
datePicker.maximumDate = max //setting max time to picker

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.

How to use one UIDatePicker to display only time on one button click and only date on another?

Please set your mode using following property as per your requirement :

@property(nonatomic) UIDatePickerMode datePickerMode

Change your mode in your button actions appropriately.

For example, lets say you have UIDatePicker *datePicker;

you can set mode using below code :

datePicker.datePickerMode = UIDatePickerModeTime;

uidatepicker always rolls to current time

Code Update:

Remove this code from ViewDidLoad()

let datepicker = UIDatePicker()
datepicker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)

and use this in ViewDidLoad()

 picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)

Move some code from buttonPressed method to datePickerChanged.

datePickerChanged will look like

@IBAction func datePickerChanged(_ sender: Any) {
updateData(date: picker.date)
}

If you want to show current date in the label then you can manager another method to set the value to the label like this

func updateData(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy MM dd hh:mm: a"
let strDate = dateFormatter.string(from: picker.date)
Label.text = strDate
}

And in ViewDidLoad() method add this

 updateData(date: Date()) // This will show the current date value

In final ViewDidLoad() will look like

override func viewDidLoad() {
super.viewDidLoad()
picker.date = Date() // You can set any date initially that you want. This will be a date object
picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
updateData(date: Date())
}

And also remove all the code from buttonPressed. Because during the initial load it will show the current time on the label.

The full code will look like

import UIKit

class testdateViewController: UIViewController {

@IBOutlet weak var Label: UILabel!
@IBOutlet weak var picker: UIDatePicker!
@IBAction func buttonPressed(_ sender: UIButton) {

}

override func viewDidLoad() {
super.viewDidLoad()
picker.date = Date() // You can set any date initially that you want. This will be a date object
picker.addTarget(self, action: #selector(testdateViewController.datePickerChanged), for: UIControl.Event.valueChanged)
updateData(date: Date())

}

@IBAction func datePickerChanged(_ sender: Any) {
updateData(date: picker.date)
}

func updateData(date: Date) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy MM dd hh:mm: a"
let strDate = dateFormatter.string(from: picker.date)
Label.text = strDate
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

UIDatePicker of type Date & Time only returns Date and not Time

You have to set timeStyle as well, e.g.

dateFormatter.dateStyle = .ShortStyle
dateFormatter.timeStyle = .ShortStyle

otherwise you'll get only the "date" part.

How to set only AM time for an UIDatePicker?

Set the date picker's minimumDate to midnight and its maximumDate to noon. Also, if (as you say) it is currently after noon, set the date picker's date to 12 hours earlier.



Related Topics



Leave a reply



Submit