Search code examples
asp.nettwitter-bootstrapbootstrap-datetimepickereonasdan-datetimepicker

Bootstrap datetimepicker - set date time server side


I have a couple of bootstrap datetime pickers and am trying to set the textbox and selected date and time from server, using data read from DB. It keeps setting it to today's date at 12:00 AM.

This is my .aspx and .aaspx.cs file content for the above:

$(document).ready(function () {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().beginAsyncPostBack();
    function EndRequestHandler(sender, args) {
        $('#tpStartDate').datetimepicker({
            sideBySide: true,
            ignoreReadonly:true,
            daysOfWeekDisabled:[0,6]
        });
        $('#tpEndDate').datetimepicker({
            sideBySide: true,
            ignoreReadonly:true,
            daysOfWeekDisabled:[0,6]
        });
    }
});
<div class="col-sm-3 col-md-3">
    <div class="form-group">
        <label for="title">Start Date</label>
        <div class="input-group date" id="tpStartDate">
            <input runat="server" id="tbStartDateTime" type="text" class="form-control" readonly="readonly" style="cursor:pointer" />
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar" style="cursor:pointer"></span>
            </span>
        </div>
    </div>
</div>

.cs:

tbStartDateTime.Value = dtClassList.Rows[0]["StartDateTime"].ToString();
tbEndDateTime.Value = dtClassList.Rows[0]["EndDateTime"].ToString();

when I put a breakpoint above, I see start datetime is "8/23/2017 1:30:00 PM" and end datetime is "8/23/2017 3:00:00 PM", but on the page they are both displayed as "10/17/2017 12:00 AM" (today's date)


Solution

  • For this you must set as option useCurrent: false as by default useCurrent is true.

    If you don't set it will always start the picker on the page with the current date and time.

    but on the page they are both displayed as "10/17/2017 12:00 AM" (today's date)

    This is an old bug: https://github.com/Eonasdan/bootstrap-datetimepicker/issues/1311

    This was solved on latest version I advise you to use V 4.17.47