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
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)")
}
}