skip to Main Content

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


  1. 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:

    Today is 2021-04-1170, 2021-01-07, 2021-01-19

    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.

    Login or Signup to reply.
    1. In Swift, normally variable names are lowercased:
    let dates : [String] = ["2021-01-07","2021-01-19"]
    
    1. To display these as-written in Text, you need to turn [String] into String. One possibility is:
    Text(dates.joined(separator: ", "))
    
    1. If you want them in a different format, you need to convert from String to Date:
    struct ContentView: View {
        @State private var formatterIn = DateFormatter()
        @State private var formatterOut = ISO8601DateFormatter()
        let dates : [String] = ["2021-01-07","2021-01-19"]
        
        var datesToNewFormat : String {
            formatterIn.dateFormat = "yyyy-MM-dd"
            return dates.compactMap { formatterIn.date(from: $0) }.map { formatterOut.string(from: $0)}
                .joined(separator: ", ")
        }
        
        var body: some View {
            VStack {
                Text(dates.joined(separator: ", "))
                Text(datesToNewFormat)
            }
        }
    }
    

    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:

    dates.map { $0 + "T00:00:00Z" }
    

    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 in Text. See https://www.hackingwithswift.com/quick-start/swiftui/how-to-format-dates-inside-text-views

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search