Convert string to unknown format today in C #

I was looking for stackoverflow for an answer, but no luck. I am developing a Windows application and I have several lines in different date formats, for example.

dd/MM/yyyy
MM/dd/yyyy
MM-dd-yyyy
dd-MM-yyyy
dd/MM/yyyy hh:mm::ss
MM/dd/yyyy hh:mm::ss
etc...

But I need to convert to a common format dd/MM/yyyy. The application can run on any Windows machine in different cultures.

What is the right way to do this?

EDIT: One more thing, I may not know what the input line format is.

Thanks in advance.

+5
source share
4 answers

Use DateTime.ParseExactwith various templates as formats.

, ToString DateTime , (, ). DateTime, -.

+8

, (.. "/" "-" ). , 10/11/2010 10 11 ? 12, , .

, , DateTime.ParseExact.

+3

, , , .

Or else: divide the “dates” into three numbers and check the range of values ​​for each of these numbers. Values> 1900 will be years. If you find values ​​from 1 to 31, it will be days. Values ​​from 1 to 12 may be months, but may also be days. Try and define each of the parts.

The best way is to ask the supplier of these dates for the format.

0
source

To run this program in a different culture, I think you should create a function to determine the culture of this string format, and then use Datetime.Parse

0
source

All Articles