DateFormatter - String to Date

struct MyView: View {
  var body: some View {

      //this value is coming from a JSON
      var myDate =  "3000-01-01T08:00:00-08:00"

      //Here I'm calling my function formatDate, passing my myDate parameter:
      Text(formatDate(dateString: myDate))
  }
}

func formatDate(dateString: String) -> Date {
  //first I dont know why my parameter gets here nil

  let dateFormatter = ISO8601DateFormatter()

  //at the dateFormatter, doesnt matter the options I set, the return is always the same. 
  dateFormatter.formatOptions = [
      .withFullDate, 
      .withFullTime,
  ]
  let finalDate = dateFormatter.date(from: dateString)!
  return finalDate
}

I just need the get the date string, convert it to the format MM dd, yyyy - hh:mm How can I do it ?

Thank you

Answered by OOPer in 686408022

I just need the get the date string, convert it to the format MM dd, yyyy - hh:mm How can I do it ?

You may need another DateFormatter to convert it to the format MM dd, yyyy - hh:mm:

struct MyView: View {
    //this value is coming from a JSON
    @State var myDate =  "3000-01-01T08:00:00-08:00"
    
    let myDateFormatter: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "MM dd, yyyy - hh:mm"
        df.timeZone = TimeZone.current
        return df
    }()
    
    var body: some View {
        Text("\(formatDate(dateString: myDate), formatter: myDateFormatter)")
    }
}
Accepted Answer

I just need the get the date string, convert it to the format MM dd, yyyy - hh:mm How can I do it ?

You may need another DateFormatter to convert it to the format MM dd, yyyy - hh:mm:

struct MyView: View {
    //this value is coming from a JSON
    @State var myDate =  "3000-01-01T08:00:00-08:00"
    
    let myDateFormatter: DateFormatter = {
        let df = DateFormatter()
        df.locale = Locale(identifier: "en_US_POSIX")
        df.dateFormat = "MM dd, yyyy - hh:mm"
        df.timeZone = TimeZone.current
        return df
    }()
    
    var body: some View {
        Text("\(formatDate(dateString: myDate), formatter: myDateFormatter)")
    }
}

The problem is when I am inside my formatDate function, if I debug mousing over the dateString variable, I can see the value "3000-01-01T08:00:00-08:00".

But when it reaches the line "let finalDate = dateFormatter.date(from: dateString)!", it returns me the error "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value"

The question is how it found nil, if when I mouse over the dateString variable, I can see the value in there ?

func formatDate(dateString: String) -> Date {
  //first I dont know why my parameter gets here nil

  let dateFormatter = ISO8601DateFormatter()

  //at the dateFormatter, doesnt matter the options I set, the return is always the same. 
  dateFormatter.formatOptions = [
      .withFullDate, 
      .withFullTime,
  ]
  let finalDate = dateFormatter.date(from: dateString)!
  return finalDate
}
DateFormatter - String to Date
 
 
Q