Error: String was not recognized as a valid DateTime in IIS Server in ASP.Net

Last Reply on Jan 16, 2017 09:46 AM By dharmendr

Posted on Jan 16, 2017 07:30 AM

Here is the datepicker:

 

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/themes/humanity/jquery-ui.css"
    rel="stylesheet" type="text/css" />

<script type="text/javascript">
    var $ = jQuery.noConflict();
    $(function () {
        $("[id$=txtExpDate]").datepicker({

            //dateFormat: 'DD, d MM, yy',
          //  dateFormat: 'mm-dd-yy',
            //buttonImageOnly: true,
            //buttonImage: 'http://jqueryui.com/demos/datepicker/images/calendar.gif'
        });
    });
    $(function () {
        $("[id$=txtManDate]").datepicker({

            //dateFormat: 'DD, d MM, yy',
          //  dateFormat: 'mm-dd-yy',
            //buttonImageOnly: true,
            //buttonImage: 'http://jqueryui.com/demos/datepicker/images/calendar.gif'
        });
    });
</script>

Here is the code for convert:

 

DateTime dt2 = Convert.ToDateTime(txtManDate.Text);

DateTime dt3 = Convert.ToDateTime(txtExpDate.Text);

The server database i am using for insert value..this working very well in local code...but not working in server.

Error is:

String was not recognized as a valid DateTime.

 

What should  i do??

 

Posted on Jan 16, 2017 07:57 AM

Hi manoj150,

Your custom format is wrong. Use DateTime.ParseExact method, your format and string should match exactly.

var manDate = DateTime.ParseExact(txtManDate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime dt2 = Convert.ToDateTime(manDate);

var expDate = DateTime.ParseExact(txtExpDate.Text, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);
DateTime dt3 = Convert.ToDateTime(expDate);

After you parse your string, you can format whatever format you want with DateTime.ToString() method.