1

I am using jQuery bootstrap ClockPicker and need to get only times that their minutes are divisible by 5. For example if user selects 13:08, the ClockPicker should select 13:05.

Is there a way to override clockpicker to round down selected times minutes?

Forough
  • 262
  • 4
  • 20
  • I don't understand the problem, by default, you can set only time with divisible by 5 minutes. http://i.stack.imgur.com/Tea6d.png – Mosh Feu Feb 14 '16 at 12:16
  • 1
    No it is not by default, you can select minutes not divisible by 5. – Forough Feb 14 '16 at 12:18

2 Answers2

2

I didn't found a way to do this with the API. So I wrote one using afterDone callback.

I parse the input's value and make the change to get the new minutes value.

var clockpicker = $('.clockpicker').clockpicker({
  afterDone: function() {
    clockpicker.val(round());
  }
}).find('input');

function round() {
  var time = clockpicker.val(),
      arr = time.split(':'),
      hour = arr[0],
      min = arr[1],
      newMin = (Math.floor(parseInt(min) / 5)) * 5;

  return hour + ':' + (newMin > 9 ? '' : '0') + newMin;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://weareoutman.github.io/clockpicker/dist/bootstrap-clockpicker.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://weareoutman.github.io/clockpicker/dist/bootstrap-clockpicker.min.js"></script>

<div class="input-group clockpicker col-xs-6">
  <input type="text" class="form-control" value="09:30">
  <span class="input-group-addon">
    <span class="glyphicon glyphicon-time"></span>
  </span>
</div>

http://jsbin.com/lopufu/edit?html,js

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • I was simply using the input's `onchange` function to do this. But `afterDone` callback seems better. Thank you. – Forough Feb 14 '16 at 12:52
1

If you don't mind make changes into Clockpicker (i had to do it for IE), adds an unofficial option like "roundmodfive" in bootstrap-clockpicker.js:
1. change this

// Hours and minutes are selected
ClockPicker.prototype.done = function() {
    raiseCallback(this.options.beforeDone);
    this.hide();
    var last = this.input.prop('value'),
        value = leadingZero(this.hours) + ':' + leadingZero(this.minutes);
    if  (this.options.twelvehour) {
        value = value + this.amOrPm;
    }
...

2. By this

// Hours and minutes are selected
ClockPicker.prototype.done = function() {
    raiseCallback(this.options.beforeDone);
    this.hide();

    var last = this.input.prop('value');
    //Unofficial option added
    /*
        Just an explain:
            I like rounding. Thus, for example 43min => 45 and 42min => 40.
            The minutes are base 60, so you have to make modulus 60.
    */
    if (this.options.roundmodfive) {
        this.minutes = (Math.round(parseInt(this.minutes) / 5)) * 5 % 60;
    }

    var value = leadingZero(this.hours) + ':' + leadingZero(this.minutes);
    if  (this.options.twelvehour) {
        value = value + this.amOrPm;
    }
...
rezequielp
  • 26
  • 4
  • Although modifying the source of a library isn't the best approach but if it's working it may count as some kind of hotfix. – Kevin Sep 26 '16 at 17:33