0

I am using jquery datetimepicker like this:

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

and when in the textbox if there is no value, it defaults to todays date and if there is a value , the datetime picker goes to 1899 as shown in the image below: enter image description here

I am using a viewmodel to bind which is below:

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


}

view:

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



}
Baahubali
  • 4,604
  • 6
  • 33
  • 72
  • seems like your string format for datetimes isn't correct either? – Grizzly Aug 29 '16 at 15:15
  • So what *exactly* is your question? Do you want it defaulting to today's date if there is no value, or do you want to stop it from going to 1899 if there is a value? – Grizzly Aug 29 '16 at 15:20
  • @BviLLe_Kid: want it to stop going to 1899 and should go to today's today or the date in the text box if possible – Baahubali Aug 30 '16 at 02:17

3 Answers3

0

You can write this code in your datetimepicker as given below. You should declared defaultDate indatetimepicker`.

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

See more details from https://eonasdan.github.io/bootstrap-datetimepicker/

Kalu Singh Rao
  • 1,671
  • 1
  • 16
  • 21
  • i did try this but it did not work. for datepicker it works but not for datetimepicker. altleast it did not work for me – Baahubali Aug 30 '16 at 05:46
  • the datetime picker doesn't even pop up now. – Baahubali Aug 30 '16 at 11:05
  • 1
    See more details from https://eonasdan.github.io/bootstrap-datetimepicker/ – Kalu Singh Rao Aug 30 '16 at 11:07
  • yea doesn't fix. some other users have this issue as well: https://github.com/smalot/bootstrap-datetimepicker/issues/207 http://stackoverflow.com/questions/3830934/jquery-ui-datepicker-back-button-jumps-to-1899 http://stackoverflow.com/questions/30104564/jquery-datetimepicker-input-text-showing-date-31-12-1899-when-time-selected – Baahubali Aug 30 '16 at 11:16
  • you can't just copy my answer and ask me to mark it as answer? – Baahubali Sep 02 '16 at 12:37
  • OK. I have no problem. I did my effort. – Kalu Singh Rao Sep 02 '16 at 12:41
0

You are getting timestamp. You can convert it to 'normal' date. But the Best way is to set date format Type in datetimepicker

0

adding useCurrent:false worked for me

$(function () {
        $('#LessonDateTime').datetimepicker({
            useCurrent: false                   
        });
    });
Baahubali
  • 4,604
  • 6
  • 33
  • 72