0

使用自定义 DateTimePicker ( https://tempusdominus.github.io/bootstrap-4/Usage/ )选择日期和时间时。当提交按钮被点击 10 次中的 9 次时,日期和时间是这样的:

01/01/0001 00:00:00

Razor 页面上的代码:

        <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>

同一页面上的脚本:

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

同一页面上的导入:

<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">

单击按钮选择日期和时间时,将设置当前日期和时间:

在此处输入图像描述

如果设置了此值,则我提交时的值为:

01/01/0001 00:00:00 
4

1 回答 1

1

正如我在评论中所说,日期选择器将第二个数字视为月份,因此您应该像这样设置自定义格式

$('#datetimepicker1').datetimepicker({
    format: 'MM/DD/YYYY HH:mm:ss',
    date: moment() //So the picker starts from now instead of the default date
});
于 2019-11-18T14:14:41.253 回答