String was not recognized as a valid DateTime “ format dd/MM/yyyy”

前端 未结 13 1500
一整个雨季
一整个雨季 2020-11-22 05:30

I am trying to convert my string formatted value to date type with format dd/MM/yyyy.

this.Text=\"22/11/2009\";

DateTime date = DateTime.Parse(         


        
13条回答
  •  忘掉有多难
    2020-11-22 06:11

    You need to call ParseExact, which parses a date that exactly matches a format that you supply.

    For example:

    DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    

    The IFormatProvider parameter specifies the culture to use to parse the date.
    Unless your string comes from the user, you should pass CultureInfo.InvariantCulture.
    If the string does come from the user, you should pass CultureInfo.CurrentCulture, which will use the settings that the user specified in Regional Options in Control Panel.

提交回复
热议问题