In my application I am having the possibility of getting date in two formats "dd/MM/yyyy" and "MM/dd/yyyy", and I want to have both date formats in "dd/MM/yyyy" format, I tried using DateTime.TryParseExact
string date = "12/21/2017";
DateTime dt;
if(!DateTime.TryParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
DateTime.TryParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
}
Console.WriteLine(dt.ToString());
My problem is that, my code works fine and if I have the date input like this 12/21/2017 (MM/dd/yyyy) and converts the date to 21/12/2017 (dd/MM/yyyy) but does not works if the date is like this 11/10/2017 which may be (MM/dd/yyyy) or (dd/MM/yyyy) it gives 10/11/2017 by considering the date format as (MM/dd/yyyy).
Is there any way to get the date format from the date so that I can use ParseExact to parse the date to (dd/MM/yyyy) format, or is there any way to convert the date formats which may be (MM/dd/yyyy) or (dd/MM/yyyy) to (dd/MM/yyyy).
Edit Based on the conversation with you, I got that it is ambiguous in certain dates, I am going to ask the Front End team to give DateTime object rather than a date string. Thank you so much.