Convert string to a specific date format

Hi! i need to convert this string : “2021-06-17” to a datetime variable, but the datetime variable needs to be exactly in this format : yyyy-MM-dd
meaning : it cant be like this 2021/06/17 (no backslash - instead of them i need hyphens (-))
i tried like this :
DateTime.ParseExact(“2021-06-17”,“yyyy-MM-dd”,Globalization.CultureInfo.InvariantCulture)
but all i get is a datetime variable like this : 2021/06/17
it must be a datetime variable exactly in the format i gave… in order to use in a soql query to salesforce. because if i put the string in that query it adds ’ ’ and than salesforce query is not working
thanks!!

Dates don’t have formats. They’re dates. The format is for when you are outputting them as a string.