javascriptjquerytwitter-bootstrapdatetimepickereonasdan-datetimepicker

How to create dynamic datetimepicker value?


My HTML code like this:

$(function () {                
    $('#datetimepicker').datetimepicker({
        minDate: moment().startOf('minute').add(300, 'm'),
    });
    
    $("#btnSubmit").click(function() {
        value = document.getElementById('datetimepicker').value;
        console.log(value)
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>


<div class="container">
    <div class="row">
        <div class='col-sm-6'>
            <div class="form-group">
                <input type='text' class="form-control" id='datetimepicker' />
            </div>
        </div>
    </div>
</div>
<button id="btnSubmit">
    Submit
</button>

Demo like this : https://jsfiddle.net/oscar11/8jpmkcr5/83/

If I click submit button, I successfully get the datetime value 300 minutes from now

But if before submitting, I wait 10 minutes, the result does not match my expectations

For example now at 6 o'clock. Before submit I wait 10 minutes. After submit, the time value I get is 11.10. So I want to like it

How can I do that? How do I make the automatic datetime value increase?

Update :

Whether the time value in textfield can be automatically increased based on current time?


Solution

  • Not sure why would you need it, but you can use something like:

    $("#btnSubmit").click(function() {
        var targetDate = new Date(new Date().getTime() + (300 * 60 * 1000));
        $('#datetimepicker').data("DateTimePicker").date(targetDate);
    
       value = document.getElementById('datetimepicker').value;
       console.log(targetDate, value);
    });
    

    On click, it will change the datetimepicker to the current date + 300 minutes. I've updated the example: https://jsfiddle.net/8jpmkcr5/86/

    EDIT: Based on your latest comment, i believe you want the use to be able to pick the date (day/month/year) but the time (hour/minutes) keep changing as time passes to current time + 3 hours. If thats the case you can use something like:

    $(function () {                
      $('#datetimepicker').datetimepicker({
            minDate: moment().startOf('minute').add(180, 'm'),
        });
    });
    
    setInterval(function(){
        var pickedDate = $('#datetimepicker').data('DateTimePicker').date();
        var currentDate = moment();
        pickedDate = pickedDate.set({
                'hour' : currentDate.get('hour') + 3,
                'minute'  : currentDate.get('minute'), 
                'second' : currentDate.get('second')
             });
        $('#datetimepicker').data("DateTimePicker").date(pickedDate);
     },1000);
    

    You can test here: https://jsfiddle.net/8jpmkcr5/98/