I'm trying to call a function one argument is current time and Date and other is a file name, They both are strings. How can I convert Date()
to a string. I tried:
writeToFile(content: Date(), fileName: "jm.txt")
but it gave me error:
Cannot convert value of type 'Date' to expected argument type 'String'
Something like this:
let df = DateFormatter() df.dateFormat = "yyyy-MM-dd hh:mm:ss" let now = df.string(from: Date()) writeToFile(content: now, fileName: "jm.txt")
You need to use DateFormatter, as stated in the docs:
Instances of
DateFormatter
createstring
representations ofNSDate
objects, and convert textual representations of dates and times intoNSDate
objects.
In your case, that would look something like this:
let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" writeToFile(content: formatter.string(from: Date()), fileName: "jm.txt")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With