String was not recognized as a valid DateTime FormatException

I get a exception with the error message "System.FormatException: String was not recognized as a valid DateTime." on the following line:

DateTime RecDate = RecDateSearch == string.Empty ? Convert.ToDateTime("1/1/1970").Date : Convert.ToDateTime(RecDateSearch).Date;

enter image description here

when choose day in the date less than 12 like 10/1/2021 or 12/12/2020, i not get any exception …etc but when choose day in the date more than 12 like 20/1/2020 or 23/12/2020 ,I get this exception.

this is my javascript code

    var myTable = $('#m_table_1').DataTable({
        "processing": true,
        "serverSide": true,
        "dom": '<"top"i>rt<"bottom"lp><"clear">',
        "scrollY": 200,
        "scrollX": true,
        "ajax": {
            "url": '@Url.Action("AllPOSDonations", "POSDonation")',
            "type": "POST"
        },
        columns: [
        { data: "Id" },
        { data: "No" },
        {
            data: "DonationDate", render: function (d) {
                return moment(d).format('YYYY/MM/DD');
        }

this is my razor

 <div class="form-group" id="datepickerDiv">
<input type="text" id="recDate" class="form-control" data-date-container='#datepickerDiv' />
</div>

35 thoughts on “String was not recognized as a valid DateTime FormatException”

Leave a Comment