Datetimepicker将圆形纪元时间戳提交为链接字段

时间:2017-08-08 21:44:21

标签: jquery timestamp datetimepicker

我使用从该库发出的datetimepicker: 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()}`);
					
				};

1 个答案:

答案 0 :(得分:0)

我终于找到了一种方法,可以使用以下内容将舍入的纪元时间戳发送到最早的分钟:

            $('.form_datetime').datetimepicker({
        language:  'fr',
        //format: 'dd/mm/yyyy hh:ii',
        linkFormat: 'dd/mm/yyyy hh:ii',
        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`);


            /* 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 */

                /* Linked field return user selection timestamp */
                $(`#${this.linkField}`).val(`${( this.getDate() || new Date() )  .getTime()}`);

                /*  /////////////////////////////////////////////////////////////////  */
                /*  Round down linked timestamp to the next minute (no second anymore) */
                /*  /////////////////////////////////////////////////////////////////  */
                // Get the linked element with id="epoch-start-time" (not rounded epoch datetime)
                var epoch_not_rounded = document.getElementById("epoch-start-time");   

                // Round down datetime to the earliest minute  (60s x 1min)
                var coeff = 1000 * 60 * 1;          
                var epoch_rounded = (epoch_not_rounded.value - ( epoch_not_rounded.value % coeff));

                document.getElementById("rounded_epoch-start-time").value = epoch_rounded;

                console.log(epoch_not_rounded.value);
                console.log(epoch_rounded);

            };

在这里工作JS小提琴示例:

https://jsfiddle.net/lcoulon/nb5hqc8j/