1

How to set hourMin, miniteMin values dynamically for jquery ui time picker, like if date is today i want allow the user to select only above the current time & if date is greater than current date then allow user to select from 0 - 23 hours.

i tried this code but not working

        $('#test1').timepicker({
            hourMin: 0,
            hourMax: 23
        });
        $("#test").datepicker({
            dateFormat: 'dd/mm/yy',
            onSelect:function(selectedDate){
                if(selectedDate == $.datepicker.formatDate('dd/mm/yy', new Date())){
                    $('#test1').timepicker('option','hourMin', new Date().getHours());
                }else{
                    $('#test1').timepicker('option','hourMin', 0);
                }
            }
        });

If there is any problem with this plugin can anybody suggest best plugin which suit my requirement.

Dyapa Srikanth
  • 1,231
  • 2
  • 22
  • 60

1 Answers1

1

If this (http://trentrichardson.com/examples/timepicker/) is the plugin that you are using, then yes, there is an option.

http://trentrichardson.com/examples/timepicker/#rest_examples

var nowDate = new Date();
$('#rest_example_3').datetimepicker({
    minDate: new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), nowDate.​getHours(),nowDate.getMinutes(), nowDate.​​​​​​​getSeconds(), 0)
});

You can set the minDateTime (or minDate) to achieve what you desire.

EDIT: A sample solution (hack) to reset the datepicker initiation on run time:

Javascript:

        function initDP()
        {
            var nowDate = new Date();
            $('#dpField').datetimepicker({
                minDate: new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), nowDate.getHours(), nowDate.getMinutes(), 0, 0)
            });
        }

        function resetDP()
        {
            $('#dpField').die();
            $('#dpField').remove();
            $('#parentId').append('<input type="text" id="dpField" />');
            initDP();
        }
        $(document).ready(function()
        {
            initDP();

            $('#resetDate').click(function(){
                resetDP();
            });
        });

    </script>

Html:

<body>
<div id="parentId">
    <input type="text" id="dpField" />
</div>
<button id="resetDate">Reset DP</button>

</body>
TJ-
  • 14,085
  • 12
  • 59
  • 90
  • thanks for the answer TJ, I am using that plugin only but i want to control that minDate or hourMin options at runtime not at initialization time. – Dyapa Srikanth Jan 02 '13 at 07:07
  • Why do you need to change it at run time? Anyway, I have added a solution. However, it is more of a hack to achieve what was asked. – TJ- Jan 02 '13 at 16:46