Disable Date After and Before Date

To disable dates after and before today in a date picker, you can use a simple approach in HTML and JavaScript.

Disables Dates After Today

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <title>Date Picker Example</title>
</head>
<body>
    <label for="datepicker">Select a date:</label>
    <input type="text" id="datepicker">

    <script>
        $(function() {
            const today = new Date();
            $("#datepicker").datepicker({
                maxDate: 0 // Disables dates after today
            });
        });
    </script>
</body>
</html>

Disables Dates Before Today


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <title>Date Picker Example</title>
</head>
<body>
    <label for="datepicker">Select a date:</label>
    <input type="text" id="datepicker">

    <script>
        $(function() {
            $("#datepicker").datepicker({
                minDate: 0 // Disables dates before today
            });
        });
    </script>
</body>
</html>

administrator

Leave a Reply

Your email address will not be published. Required fields are marked *