How To Fetch Date & Time In iOS Using Swift 3 & Xcode 8 ?

How To Fetch Date & Time In iOS

Hi, In this tutorial, I'll show you how to get the date and time in iOS apps using Swift 3 and Xcode 8.

This is very useful to learn and you can easily fetch current date & time just by using a few lines of code.

So, let's start and see how to get the date & time in Swift..

You can also watch this tutorial on my YouTube channel here: How To Get Date & Time in iOS ?



First of all, open your Xcode and create a Single View Application.



Now give some name to your project and make sure to choose Swift as the language. So, once you're in your Xcode editor, open ViewController.swift file and add these lines of code in your viewDidLoad() method:

override func viewDidLoad() {
        super.viewDidLoad()
        
        let date = Date()
        let calendar = Calendar.current
    }

Here, we're creating 2 constants for our Date() and Calendar.current . We'll use these objects to get and save the date & time in our other objects and use them in our application.

So now, to get the time, just add these lines of code in your viewDidLoad()

        let hour = calendar.component(.hour, from: date)         // 1
        let minutes = calendar.component(.minute, from: date)    // 2
        let seconds = calendar.component(.second, from: date)    // 3
        
        print("\(hour):\(minutes):\(seconds)")                   // 4


1. Here we're using calendar.component to get the current hour from our date object and saving it to our hour object.

2. Here we're using calendar.component to get the current minutes from our date object and saving it to our minutes object.

3. Here we're using calendar.component to get the current seconds from our date object and saving it to our seconds object.

4. Here, we're just printing these values in our console. You can use these values in your project/app as per your requirements.

Now, to get the current day and month, we'll use the same method. You just need to create 2 more objects for saving your current day and month. So, for this, just add these 2 more lines in your code and you'll get it:

        let day = calendar.component(.day, from: date)
        let month = calendar.component(.month, from: date)

                 print("\(day).\(month)")

Here again we're using the same method and just replacing the day and month in our calendar.current() respectively.

You can check the right day and month by printing these values in your console.

Very simple and easy, right??

So, that's all for this tutorial. Here's the full code for your reference:

_________________________________________________________________________________

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let date = Date()
        let calendar = Calendar.current
        
        let hour = calendar.component(.hour, from: date)         // 1
        let minutes = calendar.component(.minute, from: date)    // 2
        let seconds = calendar.component(.second, from: date)    // 3
        
        print("\(hour):\(minutes):\(seconds)")                   // 4
        
        let day = calendar.component(.day, from: date)
        let month = calendar.component(.month, from: date)
        
        print("\(day).\(month)")
        
    }


}
_____________________________________________________________________________


I hope this post may help someone, somehow!

If you've any doubts or need any further help, then do message me in comments section below and I'll try to respond you as soon as possible.

Thanks for your time. Keep coding...

Comments

Popular posts from this blog

How To Make The Device Vibrate Or Ring- An iOS Swift Tutorial

PayPal Integration In iOS Applications | Payment Gateways iOS/Swift

How To Parse JSON Data In iOS (Swift 3 + Xcode 8) ?