0
votes

I use the datetimepicker issued from that library : http://www.malot.fr/bootstrap-datetimepicker/

For my project i need to submit with GET form the picked date and time as an epoch format rounded at the next minute.

  • At the moment the datetime picker send a correct epoch timestamp but it includes seconds depending on the moment the user clicks on the submit button.

i would like to avoid that.

I tried to round the linked field but it does not work, seconds still included in the timestamp.

Can you tell me how to get a clean and next minute rounded timestamp without any second ?

$('.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

1 Answers

0
votes

I finally found a way to send the rounded down epoch timestamp to the earliest minutes with the following :

            $('.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);

            };

Working JS Fiddle example here :

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