disable past dates on datepicker

前端 未结 22 1191
梦毁少年i
梦毁少年i 2020-11-29 06:03

How to disable past dates from the current date on a datetimepicker? I tried few posts for similar question but was unable to achieve it, Below is what I tried



        
相关标签:
22条回答
  • 2020-11-29 06:24

    To disable all previous dates, give start date as today date

    startDate: new Date()
    

    Solution: disable all previous dates from today

    $(function() {
        $( "#datepicker" ).datepicker({ startDate: new Date()});
     });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.min.js"></script>
    
    <div> Select Date <input type="text" id="datepicker" /></div>

    Solution: disable all past dates from a particular date.

    $(function() {
        $( "#datepicker" ).datepicker({ startDate: new Date("2019-10-15")});
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.min.js"></script>
    
    <div> Select Date <input type="text" id="datepicker" /></div>

    This will disable all days before the date 15th October 2019 (2019-10-15)

    0 讨论(0)
  • 2020-11-29 06:24

    **this worked in my wordpress plugin **

     jQuery(document).ready(function($) {
               $("#datepicker").datepicker({ minDate: 0});
            });
    
    0 讨论(0)
  • 2020-11-29 06:25

    This will work:

     var dateToday = new Date();    
     $(function () {
         $("#date").datepicker({ 
             minDate: dateToday 
         });
     });
    
    0 讨论(0)
  • 2020-11-29 06:25

    If you want to set date on page load then use this:

     $('#datetimepicker1').datetimepicker({
      minDate: new Date()
      });
    

    This will set today's date as start date on page load itself and disable all the previous dates.
    But if you want to set date on click of particular text-box instead of setting it on page load then use this: $('#datetimepicker1').datetimepicker(); $("#datetimepicker1").on("click", function (e) { $('#datetimepicker1').data("DateTimePicker").minDate(new Date()); });

    In place of new Date(), we can use any string specifying Date in a format specified by us if we don't want to set current date as minimum date. e.g:

    $('#datetimepicker1').data("DateTimePicker").minDate("10/15/2018");
    
    0 讨论(0)
  • Try this'
     <link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
        <!-- table -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
        <!-- end table -->
    
        <script>
           $(function() {
            $('#example').DataTable();
            $("#from_date").datepicker({
                dateFormat: "mm/d/yy",
                maxDate: 0,
    
                onSelect: function () {
    
    
    
                    var minDate = $(this).datepicker('getDate');
    
                    $('#to_date').datepicker('setDate', minDate);
                    $('#to_date').datepicker('option', 'maxDate', 0);
                    $('#to_date').datepicker('option', 'minDate', minDate);
                }
            });
            $('#to_date').datepicker({
                dateFormat: "mm/d/yy"
            });
    
           });
           </script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
        <!-- table -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
        <!-- end table -->
    
        <script>
           $(function() {
            $('#example').DataTable();
            $("#from_date").datepicker({
                dateFormat: "mm/d/yy",
                maxDate: 0,
    
                onSelect: function () {
    
    
    
                    var minDate = $(this).datepicker('getDate');
    
                    $('#to_date').datepicker('setDate', minDate);
                    $('#to_date').datepicker('option', 'maxDate', 0);
                    $('#to_date').datepicker('option', 'minDate', minDate);
                }
            });
            $('#to_date').datepicker({
                dateFormat: "mm/d/yy"
            });
    
           });
           </script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
        <!-- table -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
        <!-- end table -->
    
        <script>
           $(function() {
            $('#example').DataTable();
            $("#from_date").datepicker({
                dateFormat: "mm/d/yy",
                maxDate: 0,
    
                onSelect: function () {
    
    
    
                    var minDate = $(this).datepicker('getDate');
    
                    $('#to_date').datepicker('setDate', minDate);
                    $('#to_date').datepicker('option', 'maxDate', 0);
                    $('#to_date').datepicker('option', 'minDate', minDate);
                }
            });
            $('#to_date').datepicker({
                dateFormat: "mm/d/yy"
            });
    
           });
           </script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
        <!-- table -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
        <!-- end table -->
    
        <script>
           $(function() {
            $('#example').DataTable();
            $("#from_date").datepicker({
                dateFormat: "mm/d/yy",
                maxDate: 0,
    
                onSelect: function () {
    
    
    
                    var minDate = $(this).datepicker('getDate');
    
                    $('#to_date').datepicker('setDate', minDate);
                    $('#to_date').datepicker('option', 'maxDate', 0);
                    $('#to_date').datepicker('option', 'minDate', minDate);
                }
            });
            $('#to_date').datepicker({
                dateFormat: "mm/d/yy"
            });
    
           });
           </script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
        <!-- table -->
        <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css">
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
        <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>
        <!-- end table -->
    
        <script>
           $(function() {
            $('#example').DataTable();
            $("#from_date").datepicker({
                dateFormat: "mm/d/yy",
                maxDate: 0,
    
                onSelect: function () {
    
    
    
                    var minDate = $(this).datepicker('getDate');
    
                    $('#to_date').datepicker('setDate', minDate);
                    $('#to_date').datepicker('option', 'maxDate', 0);
                    $('#to_date').datepicker('option', 'minDate', minDate);
                }
            });
            $('#to_date').datepicker({
                dateFormat: "mm/d/yy"
            });
    
           });
           </script>
    
    0 讨论(0)
  • 2020-11-29 06:26

    To disable past dates, Add this given js:

    var $input = $('.datepicker').pickadate();
    var picker = $input.pickadate('picker');
    picker.set('min',true);`][1]
    
    0 讨论(0)
提交回复
热议问题