英文:
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('rounded = ' + rounded);
$(document).ready(function() {
$('#meetingTime').datetimepicker({
date: rounded,
format: 'HH:mm',
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() {
$('#meetingTime').datetimepicker({
format: 'HH:mm',
stepping: 5 // optional: sets the increment of the minutes to 5
}).on('focusin', 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('DateTimePicker').date(rounded);
});
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论