【发布时间】:2018-01-16 14:57:31
【问题描述】:
我使用从该库发出的日期时间选择器: http://www.malot.fr/bootstrap-datetimepicker/
对于我的项目,我需要使用 GET 表单提交选择的日期和时间作为下一分钟四舍五入的纪元格式。
- 目前日期时间选择器发送正确的纪元时间戳,但它包括秒数,具体取决于用户单击提交按钮的时刻。
我想避免这种情况。
我尝试对链接字段进行四舍五入,但它不起作用,时间戳中仍包含秒数。
你能告诉我如何在没有任何秒的情况下获得干净且下一分钟的圆形时间戳吗?
$('.form_datetime').datetimepicker({
language: 'fr',
pickerPosition: "bottom-left",
weekStart: 1,
startDate: new Date(), // remove dates in the past
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 0,
maxView: 3,
minuteStep: 1,
});
/* Addon : Hidden linked field returns epoch time - needs moment.js library */
/* get the datetimepicker controller */
let picker = $(`.form_datetime`).data(`datetimepicker`);
var coeff = 1000 * 60 * 1;
/* override its setValue() method */
let f = picker.setValue;
picker.setValue = function(...xs) {
/* call the original method first */
f.call(this, ...xs);
/* now set the linked field to epoch format */
/* Original code without rounding timestamp
$(`#${this.linkField}`).val(`${( this.getDate() || new Date() ) .getTime()}`);
*/
/* Trying to round timestamp to the next minutes (no second anymore) */
$(`#${this.linkField}`).val(`${( this.getDate() || new Date(Math.round(date.getTime() / coeff) * coeff) ) .getTime()}`);
};
【问题讨论】:
标签: jquery timestamp datetimepicker