Error - String was not recognized as a valid dateTime RRS feed

  • Question

  • User-797751191 posted


      In Script i have below code . Datatype is Date in Sql database

    $("#txt_StartDate").datepicker({ dateFormat: 'dd/mm/yy' }).on("change", function () {
                    //get start date
                    var startdate = $(this).val();
                    //split the date to get the year and month
                    var array = startdate.split("/")
                    var year = parseInt(array[2]), month = parseInt(array[1]);
                    //set the closing date.
                    $("#txt_ClosingDate").datepicker({ dateFormat: 'dd/mm/yy' });
                    $("#txt_ClosingDate").datepicker("setDate", new Date(year, month, 0));
    On below line i get above error
    DateTime m_ClosingDate = Convert.ToDateTime(txt_ClosingDate.Text.ToString());


    Monday, February 25, 2019 11:14 AM

All replies

  • User753101303 posted


    When it happens see which string value you are trying to convert (ie which value you have in txt_ClosingDate.Text) and which culture is currently in use (ie System.Threading.Thread.CurrentThread..CurrentCulture).

    It should allow to understand why if fails (for example it could be that you have "25/02/19" while your server side culture expect "02/25/19" instead).

    Monday, February 25, 2019 1:37 PM
  • User839733648 posted

    Hi jsshivalik,

    Just as PatriceSc has said, this is because the inconsistent time format.

    A suggested method is that you could use .ParseExact(), for example.

    DateTime date = DateTime.ParseExact(this.Text, "dd/mm/yy", 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.

    You could check the official documentation: https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?view=netframework-4.7.2 

    Best Regards,


    Tuesday, February 26, 2019 8:57 AM