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

前端 未结 13 1128
一整个雨季
一整个雨季 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 05:54

    You might need to specify the culture for that specific date format as in:

        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); //dd/MM/yyyy
    
        this.Text="22/11/2009";
    
        DateTime date = DateTime.Parse(this.Text);
    

    For more details go here:

    http://msdn.microsoft.com/en-us/library/5hh873ya.aspx

    0 讨论(0)
  • 2020-11-22 05:56

    Worked for me below code:

    DateTime date = DateTime.Parse(this.Text, CultureInfo.CreateSpecificCulture("fr-FR"));
    

    Namespace

    using System.Globalization;
    
    0 讨论(0)
  • 2020-11-22 05:57

    Use DateTime.ParseExact.

    this.Text="22/11/2009";
    
    DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);
    
    0 讨论(0)
  • 2020-11-22 05:57

    Although the above solutions are effective, you can also modify the webconfig file with the following...

    <configuration>
       <system.web>
         <globalization culture="en-GB"/>
       </system.web>
    </configuration>
    

    Ref : Datetime format different on local machine compared to production machine

    0 讨论(0)
  • 2020-11-22 05:59

    Just like someone above said you can send it as a string parameter but it must have this format: '20130121' for example and you can convert it to that format taking it directly from the control. So you'll get it for example from a textbox like:

    date = datetextbox.text; // date is going to be something like: "2013-01-21 12:00:00am"
    

    to convert it to: '20130121' you use:

    date = date.Substring(6, 4) + date.Substring(3, 2) + date.Substring(0, 2);
    

    so that SQL can convert it and put it into your database.

    0 讨论(0)
  • 2020-11-22 06:02

    Parsing a string representation of a DateTime is a tricky thing because different cultures have different date formats. .Net is aware of these date formats and pulls them from your current culture (System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat) when you call DateTime.Parse(this.Text);

    For example, the string "22/11/2009" does not match the ShortDatePattern for the United States (en-US) but it does match for France (fr-FR).

    Now, you can either call DateTime.ParseExact and pass in the exact format string that you're expecting, or you can pass in an appropriate culture to DateTime.Parse to parse the date.

    For example, this will parse your date correctly:

    DateTime.Parse( "22/11/2009", CultureInfo.CreateSpecificCulture("fr-FR") );
    

    Of course, you shouldn't just randomly pick France, but something appropriate to your needs.

    What you need to figure out is what System.Threading.Thread.CurrentThread.CurrentCulture is set to, and if/why it differs from what you expect.

    0 讨论(0)
提交回复
热议问题