datetimepicker – 使用最接近的5分钟取整的时间

huangapple go评论56阅读模式
英文:

datetimepicker - use a time that is rounded to nearest 5 minutes

问题

I am using a bootstrap datetimepicker. I want to have the time rounded up to the nearest 5 minutes.

I tried:

var coeff = 1000 * 60 * 5;
var date = new Date(); //or use any other date
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
console.log('rounded = ' + rounded)
$(document).ready(
    function() {
        $('#meetingTime').datetimepicker({
            defaultDate: rounded,
            format: 'HH:mm'
        });
        $('#meetingTime').datetimepicker({
            defaultDate: rounded,
            format: 'HH:mm'
        })
    }
);

In the console I see that rounded up date object is created. However in the datetimepicker the rounded up date is not used and the current date is used.

What am I doing wrong?

英文:

I am using a bootstrap datetimepicker. I want to have the time rounded up to the nearest 5 minutes.

I tried:

var coeff = 1000 * 60 * 5;
var date = new Date(); //or use any other date
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
console.log('rounded = ' + rounded)
$(document).ready(
    function() {
        $('#meetingTime').datetimepicker({
            defaultDate: rounded,
            format: 'HH:mm'
        });
        $('#meetingTime').datetimepicker({
            defaultDate: rounded,
            format: 'HH:mm'
        })
    }
);

In the console I see that rounded up date object is created. However in the datetimepicker the rounded up date is not used and the current date is used.

What am I doing wrong?

答案1

得分: 1

这是你的代码翻译:

// 原版本:
var coeff = 1000 * 60 * 5;
var date = new Date(); // 或使用其他日期
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
console.log('rounded = ' + rounded);

$(document).ready(function() {
  $('#meetingTime').datetimepicker({
    date: rounded,
    format: 'HH:mm',
    stepping: 5 // 可选:将分钟的增量设置为5
  });
});

// 更新后版本:
$(document).ready(function() {
  $('#meetingTime').datetimepicker({
    format: 'HH:mm',
    stepping: 5 // 可选:将分钟的增量设置为5
  }).on('focusin', function() {
    var coeff = 1000 * 60 * 5;
    var date = new Date(); // 或使用其他日期
    var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
    $(this).data('DateTimePicker').date(rounded);
  });
});
英文:

My version:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var coeff = 1000 * 60 * 5;
var date = new Date(); //or use any other date
var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
console.log(&#39;rounded = &#39; + rounded);

$(document).ready(function() {
  $(&#39;#meetingTime&#39;).datetimepicker({
    date: rounded,
    format: &#39;HH:mm&#39;,
    stepping: 5 // optional: sets the increment of the minutes to 5
  });
});

<!-- end snippet -->

Update:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  $(&#39;#meetingTime&#39;).datetimepicker({
    format: &#39;HH:mm&#39;,
    stepping: 5 // optional: sets the increment of the minutes to 5
  }).on(&#39;focusin&#39;, function() {
    var coeff = 1000 * 60 * 5;
    var date = new Date(); //or use any other date
    var rounded = new Date(Math.round(date.getTime() / coeff) * coeff);
    $(this).data(&#39;DateTimePicker&#39;).date(rounded);
  });
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 18:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定