Jaylen Jaylen - 2 months ago 35
Javascript Question

How can I set the bootstrap-datetimepicker to Todays's date at 12AM

I am trying to use the bootstrap-datetimepicker to allow the users to select date and time value using an easy tool.

I created two pickers using bootstrap-datetimepicker and linked them together like so

//From Picker
$('#ActionForm_LocalFrom').datetimepicker({
format: 'MM/DD/YYYY LT',
}).on("dp.change", function (e) {
$('#ActionForm_LocalTo').data("DateTimePicker").minDate(e.date);
});

//To Picker
$('#ActionForm_LocalTo').datetimepicker({
format: 'MM/DD/YYYY LT',
useCurrent: false //Important! See issue #1075
}).on("dp.change", function (e) {
$('#ActionForm_LocalFrom').data("DateTimePicker").maxDate(e.date);
});


I need to change default date time on
$('#ActionForm_LocalFrom')
to be set to
9/24/2016 12:00 AM
and the default of
$('#ActionForm_LocalTo')
to
9/24/2016 11:59 PM
. Note that 9/24/2016 is today's date.

How can I set the date to today's date and the time to 12:00AM for the "From" picker and "11:59 PM" for the "To" Picker?

Here is what I tried here but it is not working

//From Picker
$('#ActionForm_LocalFrom').datetimepicker({
format: 'MM/DD/YYYY LT',
}).on("dp.change", function (e) {
$('#ActionForm_LocalTo').data("DateTimePicker").minDate(e.date);
}).defaultDate(moment().startOf('day'));

//To Picker
$('#ActionForm_LocalTo').datetimepicker({
format: 'MM/DD/YYYY LT',
useCurrent: false //Important! See issue #1075
}).on("dp.change", function (e) {
$('#ActionForm_LocalFrom').data("DateTimePicker").maxDate(e.date);
}).defaultDate(moment().endOf('day'));

Answer

you have to set useCurrent to false.

Try this

//From Picker
$('#ActionForm_LocalFrom').datetimepicker({
    format:  'MM/DD/YYYY LT',
    useCurrent: false,
    defaultDate: moment().startOf('day')
}).on("dp.change", function (e) {
    $('#ActionForm_LocalTo').data("DateTimePicker").minDate(e.date);
});

//To Picker
$('#ActionForm_LocalTo').datetimepicker({
    format:  'MM/DD/YYYY LT',
    useCurrent: false,
    defaultDate: moment().endOf('day')
}).on("dp.change", function (e) {
    $('#ActionForm_LocalFrom').data("DateTimePicker").maxDate(e.date);
});
Comments