convert string in unknown format to date in c#

前端 未结 4 1198
你的背包
你的背包 2021-01-13 06:45

I have searched stackoverflow for an answer but no luck. I am developing a windows application and I have some strings in different date formats, eg.

dd/MM/y         


        
4条回答
  •  孤城傲影
    2021-01-13 07:24

    You could distinguish between those formats that use different separators (i.e. "/" vs "-"). But how would you know if date such as 10/11/2010 represents 10th of November or 11th of October? If one number is not bigger than 12, there is no reliable way to do this without knowing an exact format.

    As others have pointed out, if you do know the exact format, then you can use DateTime.ParseExact.

提交回复
热议问题