0

When selecting the date and time with a custom DateTimePicker (https://tempusdominus.github.io/bootstrap-4/Usage/). When the submit button is clicked 9 out of 10 times the date and time is this :

01/01/0001 00:00:00

Code on the Razor page :

        <div class="form-group row">
            <label class="col-sm-2 col-form-label">Date and time :</label>
            <div class="col-sm-4">
                <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
                    <input asp-for="IncidentDateAndTime" type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" />
                    <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
                        <div class="input-group-text"><i class="fa fa-calendar"></i></div>
                    </div>
                </div>
            </div>
        </div>

The script on the same page :

 $(function () {
    $('#datetimepicker1').datetimepicker();
 });

The imports on the same page :

<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment-with-locales.js"></script>
<script src="https://rawgit.com/tempusdominus/bootstrap-4/master/build/js/tempusdominus-bootstrap-4.min.js"></script>
<link href="https://rawgit.com/tempusdominus/bootstrap-4/master/build/css/tempusdominus-bootstrap-4.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

When clicking on the button to select a date and time the current date and time is set :

enter image description here

If this is set the value is this when I submit :

01/01/0001 00:00:00 
user7849697
  • 503
  • 1
  • 8
  • 19
  • Have you tried changing the format? Maybe it's a default datetime because the datepicker sees "18" as the month instead of the day. Just try to select "11/11/2019 1:52 PM" – Dr. Roggia Nov 18 '19 at 12:57
  • Ok it seems that when I select 10/10/2019 or 11/11/2019 it works. But I tried to edit the format before but that didn't worked.. – user7849697 Nov 18 '19 at 13:07

1 Answers1

1

As i said in the comments, the datepicker sees the second digits as month, so you should set a custom format like so

$('#datetimepicker1').datetimepicker({
    format: 'MM/DD/YYYY HH:mm:ss',
    date: moment() //So the picker starts from now instead of the default date
});
Dr. Roggia
  • 1,095
  • 3
  • 16
  • 40