Swift3- How to get only the time of UIDatePicker


This question already has an answer here:

  • How to disable only date selection in UIDatePicker not time selection 6 answers

I'm trying to create an alarm clock app for iOS, Im new to Swift. I want to know how to get only the time from the UIDatePicker. Till now, I have a code which just gives the entire date with time. `import UIKit

class ViewController: UIViewController {

@IBOutlet weak var alarmTime: UIDatePicker!
override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.
@IBAction func alarmTime(_ sender: UIDatePicker){
 sender.datePickerMode = UIDatePickerMode.time

} ` Here is the screenshot of the output of the above code in the terminal

Set your date picker mode as below.

datePicker.datePickerMode = UIDatePickerMode.Time

Get Time from Date :

    let date = Date()

    let calendar = Calendar.current
    let comp = calendar.dateComponents([.hour, .minute], from: date)
    let hour = comp.hour
    let minute = comp.minute