I have an array of dates
let Dates = ["2021-01-07","2021-01-19"]
and I would like to display them in a Text as a sting
Text("(Dates)")
However I keep getting the error
Cannot call value of non-function type '[String]'.
I wanted to know if this doing this could be achieved in swiftUI.
I also wanted to know if I could display todays date in the format
YYYY-MM-DDT00:00:00Z.
2
Answers
Definitely!
Result:
Note that
"YYYY-MM-DDT00:00:00Z"
date format results in2021-04-1170
. You probably want something like2021-04-27T11:56:55-0700
instead. In that case, doThe
''
encloses custom characters that you want to add, likeT
. For the other format characters, check out this website.Text
, you need to turn[String]
intoString
. One possibility is:String
toDate
:Note on this last item that it deals with timezone conversion as well. In your example, if you want
T00:00:00Z
, the easiest thing would be to just append that onto the end of your original strings:Or, you could use DateComponents to manually set the hour to zero. This all probably depends on where your input is coming from and what your intent is with the output.
It is also probably worth mentioning that SwiftUI has some built-in tools for displaying
Date
inText
. See https://www.hackingwithswift.com/quick-start/swiftui/how-to-format-dates-inside-text-views