0

我正在使用这样的jquery datetimepicker:

<script type="text/javascript">
$(document).ready(function () {
    $("#LessonDateTime").datetimepicker(
    );
});
</script>

如果在文本框中没有值,则默认为今天的日期,如果有值,则日期时间选择器转到 1899,如下图所示: 在此处输入图像描述

我正在使用视图模型进行绑定,如下所示:

public class AppointmentViewModel
{
    public long? UserId { get; set; }
    public long Id { get; set; }
    public string AppointmentInstructorName { get; set; }

    [DisplayFormat(ApplyFormatInEditMode =true, DataFormatString ="{0:dd/MMM/yyyy h:mm tt}")]
    public DateTime? LessonDateTime { get; set; }

    public string AppointmentLessonAddress { get; set; }


}

看法:

 @{ViewBag.PageTitle = "Edit an Appointment";}
 <script type="text/javascript">
 $(document).ready(function () {
    $("#LessonDateTime").datetimepicker(
    );
});
</script>
 @using (Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.Hidden("userId", Model.UserId)
<div class="row">
    <div class="col-md-8">
        <div class="panel">

            <div class="panel-heading tabbable" tabindex="0"><h1>Edit Appointment Details</h1></div>

            <div class="panel-body">

                <div class="form-group row">
                    <div class="col-sm-6">
                        @Html.LabelFor(model => model.AppointmentInstructorName, "Instructor Name", htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.AppointmentInstructorName, new { htmlAttributes = new { @class = "form-control" } })
                    </div>

                </div>
                <div class="form-group row">
                    <div class="col-sm-6">

                        @Html.LabelFor(model => model.LessonDateTime, "Lesson Date and Time", htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.LessonDateTime, new { htmlAttributes = new { @class = "form-control" } })
                    </div>
                </div>
                <div class="form-group row">
                    <div class="col-sm-6">
                        @Html.LabelFor(model => model.AppointmentLessonAddress, "Address (if different)", htmlAttributes: new { @class = "control-label" })
                        @Html.EditorFor(model => model.AppointmentLessonAddress, new { htmlAttributes = new { @class = "form-control" } })
                    </div>



                </div>
                <div class="form-group row">
                    <div class="col-sm-6">
                        <input type="submit" class="btn btn-primary form-control" value="Submit" />
                        </div>
                    </div>
                </div>




        </div>
    </div>


</div>



}
4

3 回答 3

0

你得到时间戳。您可以将其转换为“正常”日期。但最好的方法是在 datetimepicker 中设置日期格式类型

于 2016-08-30T06:11:52.020 回答
0

您可以在 datetimepicker 中编写此代码,如下所示。你应该声明defaultDate indatetimepicker`。

 $(function () {
            $('#LessonDateTime').datetimepicker({
                defaultDate: "11/1/2013",
                useCurrent: false,                   
            });
        });

从https://eonasdan.github.io/bootstrap-datetimepicker/查看更多详细信息

于 2016-08-30T05:37:11.390 回答
0

添加useCurrent:false对我有用

$(function () {
        $('#LessonDateTime').datetimepicker({
            useCurrent: false                   
        });
    });
于 2016-08-30T12:28:04.817 回答