find the current time in swift

To find the current time in Swift, you can use the Date() function to get the current date and time, and then format it using a DateFormatter object. Here's an example:

main.swift
let now = Date()
let formatter = DateFormatter()
formatter.timeStyle = .medium
let currentTime = formatter.string(from: now)
print("The current time is \(currentTime)")
169 chars
6 lines

This will output the current time in a medium format, such as "12:34:56 PM". You can adjust the format by changing the timeStyle property to .short or .long, or by using a custom format string with the dateFormat property.

related categories

gistlibby LogSnag