Home > Back-end >  Convert String to type LocalDate at Swift
Convert String to type LocalDate at Swift

Time:04-28

Can someone help how to convert a document to this format? This is not the classic Swift format - Date Some localDate?

enter image description here

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd'"
let dateForSend = dateFormatter.date(from: date)
let parameters: Parameters = [ "date": dateForSend]

I try this one, but it's not working, server send to me:

"{\"status\":\"BAD_REQUEST\",\"message\":\"Failed to convert value of type \'java.lang.String\' to required type \'java.time.LocalDate\' ...

CodePudding user response:

Try changing your date format

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

// this is to get the string
let string = dateFormatter.string(from: Date())

// prints '2022-04-27'
print(string) 

let date = dateFormatter.date(from: "2022-04-27")

// Optional(2022-04-27 07:00:00  0000)
print(date)

CodePudding user response:

I needed to be more careful and pay attention to the fact that the type I had to pass was a String type in a certain format, and not a Date type

zoomed image

  • Related