Find out the Date time format of string date in c#

后端 未结 4 1447
傲寒
傲寒 2020-12-21 00:21

I am making an application. The application uses date format such as \"2012-11-21 15:22:35\".
I already know that the format is \"yyyy-MM-dd HH:mm:ss\

4条回答
  •  时光取名叫无心
    2020-12-21 01:17

    I think you would run into problems with date strings where the month value is <= 12 as this means that the format of that string could be "yyyy-MM-dd" or "yyyy-dd-MM"

    There is no way of knowing which one is the correct one unless you put a preference in your parser.

    For example: 2012/08/07 - "could this be July or August?"

    You could just brute-force it and hope there is a CultureInfo matching the format

     var datestring = "2012-10-05 12:00:03";
     DateTime time;
     var matchingCulture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(ci => DateTime.TryParse(datestring, ci, DateTimeStyles.None, out time))
    

提交回复
热议问题