Converting a string to DateTime object

前端 未结 5 615
轮回少年
轮回少年 2021-01-16 15:57

I want to convert a string in a specific format to a DateTime.

My specific string format is \"dd-mm-yyyy\".

I cannot do any string

5条回答
  •  失恋的感觉
    2021-01-16 16:21

    This sounds like a localization problem - if that is the case I recommend using one of the overloads of DateTime.parse that accepts a System.IFormatProvider. From the docs:

    Type: System.IFormatProvider
    An object that supplies culture-specific format information about s. 
    

    This also allows your code to be flexible - if the date format ever changes in the future to support a different culture, you can just change the format provider.

提交回复
热议问题