0

When the user add the question, I want to store it in database.One of the column of database is "Time" having datatype datetime.

<div asp-validation-summary="All" role="alert" style="display:none"></div>
<div class="qs-container mt-5 mb-5 py-5">
        <h1>Ask a Public Question</h1>
        <form  method="post" action="/User/AskQuestion" id="question_form" class="d-flex justify-content-space-between align-items-center" style="flex-direction: column;">
            <div class="title-div">
                <label asp-for="Title">Question Title:</label>
                <br>
                <input type="text" asp-for="Title" id="title1">
                <br>
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="qs-div">
                <label asp-for="Description">Description:</label>
                <br>
                <textarea asp-for="Description" id="summernote"></textarea>
                <br>
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
            <div class="opt-grp">
                <label asp-for="Subject">Choose Subject:</label>
                <br>
                <select asp-for="Subject" id="subject">
                    <option selected="selected" value="Exam">Exam</option>
                    <option value="Admission">Admission</option>
                    <option value="">Job</option>
                    <option value="Study">Study</option>
                    <option value="">Information</option>
                    <option value="">Events</option>
                </select>
            </div>
            <input asp-for="QuestionaireId" value="@UserId"/>
            <input id="question_time" asp-for="Time"/>
            <button class="btn btn-success submit-btn" value="submit">Submit</button>
        </form>
    </div>

and now what i am doing is to change value of input before submitting the form.

    <script>
              $('#summernote').summernote({
        placeholder: 'Description goes here....',
        height:'25vh',
        toolbar: [
          ['style', ['style']],
          ['font', ['bold', 'underline', 'clear']],
          ['color', ['color']],
          ['para', ['ul', 'ol', 'paragraph']],
          ['table', ['table']],
          ['insert', ['link', 'picture']],
          ['view', ['codeview', 'help']]
        ]
      });
      $('#question_form').submit(function(){
          let currdate = new Date();
          let currdatetime=currdate.toLocaleString();
          $('#question_time').val(currdatetime)
          alert($('#question_time').val());
          return true;
      })
    </script>

but the value of question_time after submitting is still null.and database give this error:

SqlTypeException: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.

1 Answers1

0

The exception you got is the format of date you pass to the date field is wrong. By default date uses YYYY-MM-DD. So in order to retrieve date first convert date object to toISOString() then using substring get only date part.

$('#question_form').submit(function(){
    let currdate = new Date();
    let currdatetime = currdate .toISOString().substr(0, 10);
    $('#question_time').attr('value',currdatetime);
    alert($('#question_time').val());
    return true;
});

More information in this Stackoverflow question

Abdul Haseeb
  • 514
  • 4
  • 13
  • I am using tolocalstring() method that turn this into form "09/08/2014, 2:35:56 AM" I think that not a case Thanks anyways – Usman Arshad Aug 10 '22 at 13:11
  • That is the case. `input type date` only accepts `yyyy-dd-mm`. replace `$('#question_time').val(currdatetime)` line with `$('#question_time').attr("value",currdatetime);` `.val()` set the value property of element and `attr('value',value)` will alter the value attribute of that element. – Abdul Haseeb Aug 10 '22 at 13:37