Swift
Swift 3
Date
Programming
Code Examples

Get current date in Swift 3?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, getting the current date and time is as simple as creating a Date() value. That gives you the current instant. If you want to display it to users, you then format that Date with DateFormatter or related Foundation APIs.

The basic answer

swift
1import Foundation
2
3let now = Date()
4print(now)

Date() captures the current moment. The printed representation is mostly useful for debugging, not for polished user-facing output.

Formatting the current date

To display the date in a readable form, use DateFormatter.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.dateStyle = .medium
5formatter.timeStyle = .short
6
7let currentDateString = formatter.string(from: Date())
8print(currentDateString)

This uses localized formatting rules, which is usually better than hardcoding a date pattern for user interfaces.

Custom format strings

If you need a specific format for logs, file names, or API-adjacent text, set dateFormat directly.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
5
6print(formatter.string(from: Date()))

That is useful for machine-readable or convention-driven output, but for user-facing text you should still prefer localized styles where possible.

Current date versus date components

Sometimes you do not want the full timestamp. You want specific parts such as year, month, or day.

swift
1import Foundation
2
3let now = Date()
4let calendar = Calendar.current
5
6let year = calendar.component(.year, from: now)
7let month = calendar.component(.month, from: now)
8let day = calendar.component(.day, from: now)
9
10print(year, month, day)

This is the right approach when you need calendar-based logic rather than a formatted string.

Time zone and locale matter

A Date represents an instant in time, not a formatted local calendar string. Formatting is where locale and time zone come into play.

That distinction is important because many “date bugs” are really formatting or calendar-interpretation bugs rather than problems with how the current instant was obtained.

swift
1import Foundation
2
3let formatter = DateFormatter()
4formatter.timeZone = TimeZone(identifier: "UTC")
5formatter.locale = Locale(identifier: "en_US_POSIX")
6formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
7
8print(formatter.string(from: Date()))

This kind of configuration is useful when you need predictable cross-system formatting rather than locale-sensitive display.

Swift 3 versus newer Swift versions

The question mentions Swift 3, but the basic answer is still the same in later Swift versions because Foundation.Date remains the standard representation of the current instant.

So even though syntax elsewhere in the language evolved, Date() is still the core answer.

Practical examples

User-facing timestamp:

These examples show why the question is often two questions at once: first get the current instant, then decide which calendar or display form your application actually needs.

swift
1let formatter = DateFormatter()
2formatter.dateStyle = .long
3formatter.timeStyle = .medium
4let labelText = formatter.string(from: Date())

File stamp:

swift
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd_HHmmss"
let fileName = "backup_\(formatter.string(from: Date())).txt"

These show why the “get current date” question often really means “get it and then format it correctly for a specific use.”

Common Pitfalls

A common mistake is printing Date() directly and expecting user-friendly output. The default debug representation is not a polished display format.

Another mistake is hardcoding a dateFormat for UI text when localized styles would be more appropriate.

A third mistake is forgetting that formatting behavior depends on locale and time zone even though the underlying Date value represents one absolute instant.

Summary

  • Use Date() to get the current date and time.
  • Use DateFormatter to turn that value into readable text.
  • Use Calendar when you need specific components such as year or day.
  • Set locale and time zone deliberately when the output format must be stable.
  • The basic Date() approach applies in Swift 3 and later Swift versions.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.