Home > Enterprise >  How can I convert a date to a string without using DateFormatter?
How can I convert a date to a string without using DateFormatter?

Time:11-10

How can I convert Date.now to a string without using DateFormatter?

CodePudding user response:

Using the new .formatted() syntax:

Date.now.formatted()
Date.now.formatted(date: .abbreviated, time: .omitted)

And so on.

CodePudding user response:

Yes. You could:

let date = Date()
let dateString = date.formatted()
print("Today is \(dateString), also work with \(date) directly.")

But you miss the control to format the date ;-)

EDIT: Sorry, I responded at the same time as @jrturton

  • Related