Set default time in bootstrap-datetimepicker

前端 未结 13 2188
后悔当初
后悔当初 2020-12-03 10:21

I want to set default time in this datetimepicker as 00:01 for the current date. Anyone tried that before? Having a tough time with it. It Seems simple.

相关标签:
13条回答
  • 2020-12-03 10:35

    This is my solution for your problem :

    $('#startdatetime-from').datetimepicker({
    language: 'en',
    format: 'yyyy-MM-dd hh:mm'
    }).on('dp.change', function (e) {
            var specifiedDate = new Date(e.date);
            if (specifiedDate.getMinutes() == 0)
            {
                specifiedDate.setMinutes(1);
                $(this).data('DateTimePicker').date(specifiedDate);
            }
        });
    

    This also work for setting a default hour or both. Note that this code sample works with the useCurrent: false parameter too.

    When the user pick a date, we check if the minute is 0 (hard-coded default value). In this case, we set a 1. Then, the user cannot select 0.

    0 讨论(0)
  • 2020-12-03 10:38

    Momentjs.com has good documentation on how to manipulate the date/time in relation to the current moment. Since Momentjs is required for the Datetimepicker, might as well use it.

    var startDefault = moment().startof('day').add(1, 'minutes');
    $('#startdatetime-from').datetimepicker({
        defaultDate: startDefault,
        language: 'en',
        format: 'yyyy-MM-dd hh:mm'
    });
    
    0 讨论(0)
  • 2020-12-03 10:42

    This works for me after a long hours of search , This solution is for Bootstrap datetimepicker version 4. when you have to set the default date in input field.

    HTML

    "input type='text' class="form-control" id='datetimepicker5' placeholder="Select to date" value='<?php echo $myDate?>'// $myDate is having value '2016-02-23' "
    

    Note- If You are coding in php then you can set the value of the input datefield using php, but datetimepicker sets it back to current date when you apply datetimepicker() function to it. So in order to prevent it, use the given JS code

    JAVASCRIPT

    <script>
    $('#datetimepicker5').datetimepicker({
               useCurrent: false //this is important as the functions sets the default date value to the current value 
               ,format: 'YYYY-MM-DD'
            });
    </script>
    
    0 讨论(0)
  • 2020-12-03 10:44

    None of the above worked for me, however I had success setting the default at time of instantiation.

    JQuery

    <script type="text/javascript">
        $(function () {
            var dateNow = new Date();
            $('#datetimepicker').datetimepicker({
                defaultDate:dateNow
            });
        });
    </script>
    
    0 讨论(0)
  • 2020-12-03 10:48

    This Works for me. I have to use specially date format like this 'YY-MM-dd hh:mm' or 'YYYY-MM-dd hh:mm'

    $('#datetimepicker1').datetimepicker({
                format: 'YYYY-MM-dd hh:mm',
                defaultDate: new Date()
            });
    
    0 讨论(0)
  • 2020-12-03 10:51

    I solved my problem like this

    $('#startdatetime-from').datetimepicker({
    language: 'en',
    format: 'yyyy-MM-dd hh:mm',
    defaultDate:new Date()
    });
    
    0 讨论(0)
提交回复
热议问题