1

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.
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Code Beast
  • 61
  • 10

2 Answers2

2
  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

jnpdx
  • 45,847
  • 6
  • 64
  • 94
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.

aheze
  • 24,434
  • 8
  • 68
  • 125
  • How would I show only 1 date? something like print(dates(0))? would this work? – Code Beast Apr 27 '21 at 19:07
  • @JonathanSoliman if you only want the first date, just replace the entire `ForEach` block with `Text("\(dates[0])")` (use square brackets `[]` to access an element at the index). Or do you want to combine all the dates into 1 string? Check out @jnpdx's `Text(dates.joined(separator: ", "))` – aheze Apr 27 '21 at 19:08
  • Also is it possible if I could make the hours minutes and seconds 0 by default? I am trying to get the difference between 2 dates and its giving me a decimal – Code Beast Apr 27 '21 at 19:18
  • @JonathanSoliman check out [this](https://stackoverflow.com/q/27182023/14351818) - instead of making it `0`, you should compare the dates by a date component (for example `day` or `month`) – aheze Apr 27 '21 at 19:29