Search code examples
twitter-bootstrapdatedefaultdatetimepicker

Bootstrap DateTime Picker set default date to yesterday


Does anyone know how to set the bootstrap datetimepicker to default date be yesterday? That is the highlighted day when the calendar pops up. I am using the one that has documented options here but having a tough time figuring out how to do it: https://eonasdan.github.io/bootstrap-datetimepicker/Options/ I was trying something like this but not sure if I am on the right track. Thanks!

    $(".datetimepicker").datetimepicker(({ language: "en", pickTime: true}));

    var date = new Date();
    date.setDate(date.getDate() - 1);
    var day = date.getDate();
    var month = date.getMonth() + 1;
    var year = date.getFullYear();

    $('#datetimepicker').datetimepicker({
        defaultDate: day+"/"+month+"/"+year //<--change the order if other format is set, this is DD/MM/YYYY Format
    });

Solution

  • I would recommend using moment subtract to manipulate the date as desired.

    $(function () {
        $("#datetimepicker1").datetimepicker({
            format: "MM/DD/YYYY - HH:mm",
            defaultDate: moment().subtract(1, 'days'),
            useCurrent: false
        });
    });
    

    see fiddle: http://jsfiddle.net/spasticdonkey/bkqx96bk/1/