【问题标题】:Datetimepicker submits rounded epoch timestamp as linked fieldDatetimepicker 提交四舍五入的纪元时间戳作为链接字段
【发布时间】: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


    【解决方案1】:

    我终于找到了一种将四舍五入的纪元时间戳发送到最早分钟的方法:

                $('.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 Fiddle 示例:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2015-01-17
      • 2018-06-03
      • 2011-01-22
      • 1970-01-01
      • 2015-02-04
      相关资源
      最近更新 更多