Source Freeze

  • Home
  • iOS
  • Swift
  • Cross-Platform
  • About
  • Contact
Home » iOS DatePicker tutorial (UIDatePicker) using Swift

iOS DatePicker tutorial (UIDatePicker) using Swift

January 4, 2023 by Source Freeze Leave a Comment

iOS DatePicker control introduction

In the Previous tutorial, we created UISegmentedControl example, now this project going to implement how to create an “iOS DatePicker” example using swift.

UIDatePicker object using to select the specific date and time. If you are a documentation lover, you can read the UIDatePicker apple documentation here, a good example of iOS DatePicker Control is the calendar app of the iPhone.

iPhone Calendar application example ios date picker

This UIDatePicker Example is written in swift so you must need Xcode6 or greater version to run the application.

Open the Xcode and create the single view application, enter the project name as “UIDatePicker Example”, fill out the Organization Name and Organization Identifier fields, next select the “Swift” in the language field and remains Use Core Data field as unselected because we are not using that feature in this project then select next and save the project.

iOS DatePicker Example using single view application

 Adding UIDatePicker into storyboard

Open the Main.storyboard file, then drag the iOS DatePicker control and UILabel into the storyboard it will looks like below,

adding iOS DatePicker into storyboard

Next, center align iOS DatePicker by selecting the align label listed at the bottom of the storyboard view, click the check box of Horizontal center in container and Vertical center in a container, now select the “Add 2 Constraints” as shown in the below now UIDatePicker control added into the center of the view.

iOS DatePicker add constraints

Then go to the ViewController.swift file by selecting the assistance editor, create the IBOutlet for UIDatePicker control and name it as “myDatePicker”, it looks like the below.

IBOutlet for iOS DatePicker Control

Next Create the IBOutlet for UILabel and name it as “selectedDate”, like below.

IBOutlet for Selected Date

Now create a IBAction for myDatePicker and name it as “datePickerAction”  it looks like below.

iOS DatePicker Control IBAction added

Next, add the below code into the “datePickerAction” method in the ViewController.swift file

@IBAction 
func datePickerAction(sender: AnyObject) 
{ 
var dateFormatter = NSDateFormatter() 
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm" 
var strDate = dateFormatter.stringFromDate(myDatePicker.date) 
self.selectedDate.text = strDate 
}

Here, we are using dateFormatter object to format the date, and converting the date to a string using stringFromDate Object, then assigning that string value into selectedDate label text.

Then go ahead, Build and Run the application, you will get the below output in the selected device or simulator.

iOS DatePicker final output

Now, you can download complete iOS Datepicker Source Code tutorial here. !!!

Filed Under: ios-tutorial, swift, uidatepicker

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Posts

  • How to Generate random numbers in JavaScript
  • How to Scroll to an Element in a React Component?
  • How to get a Date without the Time in JavaScript
  • How to modify url without reloading the page using javascript
  • How to disable server side rendering in nextjs
  • How to get a file type from URL in JavaScript

Recent Comments

    Tags

    beginner colon cross-platform es6 function html ionic iOS javascript mobile application development nextjs objective-c swift switch ternary typescript uiwebview Visual Studio plugin web developer

    Copyright © 2025 Source Freeze