Search code examples
javascriptcssbootstrap-datetimepickereonasdan-datetimepicker

Insert label inside bootstrap datetimepicker


Is there a way to add hour/minute label to eonasdan's bootstrap datetimepicker like shown in the screenshot?

enter image description here


Solution

  • As ceejayoz stated in comments, the datetimepicker does not include this kind of functionality. The library is open source and you can customize the code and the look & feel as suggested.

    Another appoach is to add dinamically a row under hours and minutes row when the picker is shown. You can add a listner to dp.show event and then use jQuery to manipulate picker HTML.

    Here a working sample:

    $('#datetimepicker1').datetimepicker({
      format: 'HH:mm'
    });
    
    $('#datetimepicker1').on('dp.show', function(){
      // Check timepicker is visible
      if( $('.timepicker').is(':visible') ){
        // Get rows of the timepicker
        var rows = $('.timepicker>.timepicker-picker>table>tbody>tr');
        // Create html for custom text
        var tr = '<tr class="customText"><td>hours</td><td class="separator"></td><td>minutes</td></tr>';
        // Add custom HTML inside component
        $(rows[1]).after(tr);
      }
    });
    .bootstrap-datetimepicker-widget table tr.customText td {
      height: 24px;
      line-height: 24px;
    }
    <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
    <link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
    
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
    
    
    <div class='input-group date' id='datetimepicker1'>
      <input type='text' class="form-control" />
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
    </div>

    The datetimepicker has the debug option that, if set to true:

    Will cause the date picker to stay open after a blur event.