Get the Current Date and Time in Swift in every possible format

In this tutorial, we will learn how to show the current date and time in various ways in Swift.

As a developer, you need to show the current time in any format as per your project requirements. Thus I will show examples of each and every possible datetime format in this tutorial.

Get current time in “YYYY-MM–DD  HH:MM:SS +TIMEZONE” format in Swift

This is the easiest way to show the current date-time.

We can simply show this by using the below code.

print (Date())

The output will be like this:

2021-12-08 06:03:40 +0000

Look at the below code, I will show you an interesting fact.

let mytime = Date()
print (mytime)

Run this code online
You can also use var instead of let as per your requirement.

Can you guess what will be the output in XCode?

If you run the above code to the current date time in XCode then the output will be like this:

Dec 8, 2021 at 11:37 AM
But if you run the same code in Swift in Print function the output will be like this:

2021-12-08 06:07:36 +0000

Get the Current Date and Time in Swift in every possible formats

In Xcode, it is showing your System’s current time which is different in Print function.

Change format of Date() in Swift

This portion will be the best part for you as here we will learn how easily we can change date format in Swift.

In swift, they have already provided DateFormatter class to do this.

Print current date and time in the shortest format

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .short
format.dateStyle = .short
print(format.string(from: mytime))

Run this program online
Output:

12/8/21, 11:52 AM

Get current date and time in a medium format which is Month D, Year

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .medium
format.dateStyle = .medium
print(format.string(from: mytime))

Execute the code online
Output:

Dec 8, 2021 at 12:06:28 PM

Get date time in biggest format with timezone in Swift

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .long
format.dateStyle = .long
print(format.string(from: mytime))

Execute the program online

Output:

December 8, 2021 at 12:11:27 PM GMT+5:30

Get only calendar date with month and year without time in Swift

This is how we can exclude time from Date() in swift

We just need to assign .timeStyle to .none as below.

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .none
format.dateStyle = .long
print(format.string(from: mytime))

Execute the program

Output:

December 8, 2021

Get only time in Swift – Exclude date from time

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .long
format.dateStyle = .none
print(format.string(from: mytime))

Run the program

Output:

12:17:40 PM GMT+5:30

How to remove timezone from Date() in Swift

let mytime = Date()
let format = DateFormatter()
format.timeStyle = .short
format.timeZone = .none
format.dateStyle = .long
print(format.string(from: mytime))

Run the program

Output:

December 8, 2021 at 12:20 PM

Change date format to YYYY-MM-DD in Swift

We can simply format the date using .dateFormat

let mytime = Date()
let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd"
print(format.string(from: mytime))

Run the program

Output:

2021-12-08

Change date format to DD-MM-YYYY in Swift

let mytime = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy"
print(format.string(from: mytime))

Run the code

Output:

08-12-2021

I think that there is no need to put examples for other formats. Instead of that, I can give you one last example for the time:

let mytime = Date()
let format = DateFormatter()
format.dateFormat = "HH:mm:ss"
print(format.string(from: mytime))

Output will be like this:

12:33:32

Using the above examples, we hope that you can build any date-time format in Swift programming.

If you still want to get ultimate formats, then one last thing that you can do is to extract elements from the date time separately and do whatever you want to do with those.

How to extract elements from date and time in Swift

Here I am showing you how to fetch only “month” from date.

var mytime = Date()
var myCalendar = Calendar.current
var currentMonth = myCalendar.component(.month, from: mytime)
print(currentMonth)

Run the code

Output:

12

As this is December.

Also read: How to generate random numbers in Swift

 

Leave a Reply

Your email address will not be published.