find the current date in swift

To find the current date in Swift, you can use the Date() constructor which creates a new instance of the Date object set to the current date and time. Here's an example:

main.swift
let currentDate = Date()
25 chars
2 lines

However, keep in mind that Date() retrieves the current time according to the device's CLOCK. This could lead to incorrect/inaccurate time representation due to device's clock offset.

It's usually best practice to manipulate timezones using a Calendar instance.

main.swift
let calendar = Calendar.current

let dateComponent = calendar.dateComponents([.year, .month, .day], from: Date())

let currentYear = dateComponent.year
let currentMonth = dateComponent.month
let currentDay = dateComponent.day

print("\(currentDay)-\(currentMonth)-\(currentYear)")
281 chars
10 lines

This will print the current date in the format "day-month-year".

gistlibby LogSnag