Skip to content

Latest commit

 

History

History
35 lines (25 loc) · 747 Bytes

convert-a-date-to-a-string.md

File metadata and controls

35 lines (25 loc) · 747 Bytes

Convert A Date To A String

Category: Swift

You can convert a date to a localised string in Swift with the following code:

print(Date().description(with: .current))

Output:

Tuesday, 17 October 2023 at 10:02:28 PM New Zealand Daylight Time

To convert to ISO8601 format showing UTC, use the following code:

let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime,
                            .withDashSeparatorInDate,
                            .withFullDate,
                            .withFractionalSeconds,
                            .withColonSeparatorInTimeZone]

// ...     
print(formatter.string(from: Date()))

Output:

2023-10-17T09:15:38.115Z