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.
I wanted to know if this doing this could be achieved in swiftUI.
Definitely!
struct ContentView: View {
let dates = ["2021-01-07", "2021-01-19"]
var body: some View {
VStack {
Text("Today is \(todayDate())")
ForEach(dates, id: \.self) { date in
Text("\(date)")
}
}
}
func todayDate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "YYYY-MM-DDT00:00:00Z"
let today = Date() /// Date() is the current date
let todayAsString = dateFormatter.string(from: today)
return todayAsString
}
}
Result:
Note that "YYYY-MM-DDT00:00:00Z"
date format results in 2021-04-1170
. You probably want something like 2021-04-27T11:56:55-0700
instead. In that case, do
dateFormatter.dateFormat = "YYYY-MM-d'T'HH:mm:ssZ"
The ''
encloses custom characters that you want to add, like T
. For the other format characters, check out this website.