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 4 Comments

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

Comments

  1. Evandro Guimarães Demuth says

    June 6, 2015 at 12:40 am

    Thanks for the tutorial. Simple and perfect!

    Reply
  2. Sam says

    September 5, 2015 at 11:31 am

    How would you capture a selected date as a variable and then access that variable from another ViewController?

    Reply
  3. Ariel says

    June 30, 2016 at 3:21 am

    Thanks, helped me a lot

    Reply
  4. Jiro Lin says

    August 19, 2016 at 8:21 am

    simple and easy to understand!
    But I have two questions, 1.how can i hide the datepicker, and show it when i click the button?
    2. How can I activate the datepicker with two text fields (start date value & end date value)

    Reply

Leave a Reply Cancel reply

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

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Posts

  • Parse Float with 2 decimals in javascript
  • Best Next JS courses to learn Next JS in 2023
  • How to redirect to another page in next js
  • How to get the query parameters from URL in Next JS?
  • iOS DatePicker tutorial (UIDatePicker) using Swift
  • UIAlertController iOS 8 using Swift

Recent Comments

  • zulfi on iOS UIPickerView Example using Swift
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework
  • SourceFreeze on Cordova InAppBrowser Plugin Example using ionic framework
  • Muhsin on Cordova InAppBrowser Plugin Example using ionic framework

Tags

cross-platform ionic iOS javascript mobile application development nextjs objective-c swift uiwebview Visual Studio plugin

Copyright © 2023 Source Freeze