US and UK format in datetime output

C#:

I want to get date time object dependent to culture specific.

Input can be in dd/mm/yyyy or mm/dd/yyyy.

Output should be culture specific.

If input : en-US and format of string is (dd/mm/yyyy , mm/dd/yyyy)

Output should be MM/dd/yyyy

If input : en-GB format of string is (dd/mm/yyyy , mm/dd/yyyy)

Output should be dd/MM/yyyy

Please provide solution in C# and provide only when both scenarios exists in solution

Jon Skeet
people
quotationmark

I want to get date time object dependent to culture specific.

There's no such concept. Even the calendar system isn't part of DateTime. The same DateTime value is used to represent a single value regardless of culture.

However, you can specify the culture when you format a DateTime, converting it to text. For example:

DateTime dateTime = ...;
string text = dateTime.ToString("d", culture); // d = short date format

Now if your input is in either dd/MM/yyyy or MM/dd/yyyy format, then you've fundamentally got a problem. You won't know whether 06/07/2015 is July 6th or June 7th, unless you have other information to help you. Again, if you know that it's the short date format of a specific culture, then you can use

DateTime dateTime = DateTime.ParseExact(text, "d", culture);

people

See more on this question at Stackoverflow