25

I am trying to restrict past dates in input type="date". I am able to restrict future dates, but how can I restrict past dates?

$(function(){
    var dtToday = new Date();

    var month = dtToday.getMonth() + 1;
    var day = dtToday.getDate();
    var year = dtToday.getFullYear();
    if(month < 10)
        month = '0' + month.toString();
    if(day < 10)
        day = '0' + day.toString();

    var minDate= year + '-' + month + '-' + day;

    $('#txtDate').attr('min', minDate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="txtDate" />
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user7397787
  • 1,410
  • 7
  • 28
  • 42

13 Answers13

23

You can try this

 var maxDate = year + '-' + month + '-' + day;
    alert(maxDate);
    $('#txtDate').attr('min', maxDate);

$(function(){
    var dtToday = new Date();
    
    var month = dtToday.getMonth() + 1;
    var day = dtToday.getDate();
    var year = dtToday.getFullYear();
    if(month < 10)
        month = '0' + month.toString();
    if(day < 10)
        day = '0' + day.toString();
    
    var maxDate = year + '-' + month + '-' + day;

    // or instead:
    // var maxDate = dtToday.toISOString().substr(0, 10);

    alert(maxDate);
    $('#txtDate').attr('min', maxDate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="txtDate" />
Dhiraj
  • 1,430
  • 11
  • 21
22

Here is a PHP solution that gets today's date and sets it as the minimum.

<input type="date" id="txtDate" min="<?php echo date("Y-m-d"); ?>">

This will put it in the correct double-digit format for the day and month. https://www.php.net/manual/en/function.date.php

Painguin
  • 1,027
  • 14
  • 26
20

Programmatically you can do something like this to disable past dates from being selectable:

<input type='date' min={new Date().toISOString().split('T')[0]} />
Muhamed Krasniqi
  • 1,134
  • 1
  • 13
  • 20
18

The below code may help you. It is normal HTML5 code:

Enter a date before 1980-01-01:

<input type="date" name="bday" max="1979-12-31">

Enter a date after 2000-01-01:

<input type="date" name="bday" min="2000-01-02">

See this working example.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
umishra
  • 390
  • 2
  • 14
4

In HTML:

<input type="date" id="ExpiryDate" class="form-control" min="9/10/2018"/>

Using Jquery new version:

function SetMinDate() {
    var now = new Date();

    var day = ("0" + now.getDate()).slice(-2);
    var month = ("0" + (now.getMonth() + 1)).slice(-2);

    var today = now.getFullYear() + "-" + (month) + "-" + (day);

    $('#ExpiryDate').val(today);
    $('#ExpiryDate').attr('min', today); }
Wajid khan
  • 842
  • 9
  • 18
2
<input type="date" id="date"> 
var date = new Date().toISOString().slice(0,10);

//To restrict past date

$('#date').attr('min', date);

//To restrict future date

$('#date').attr('max', date);
1
  $(function() {
          $(document).ready(function () {
            var todaysDate = new Date();
            var year = todaysDate.getFullYear();
            var month = ("0" + (todaysDate.getMonth() + 1)).slice(-2);
            var day = ("0" + todaysDate.getDate()).slice(-2);
            var maxDate = (year +"-"+ month +"-"+ day);
            $('.class_of_input_field').attr('min',maxDate);
          });
        });``
Shah Xahur
  • 11
  • 2
  • 2
    Hi! It would make your answer more useful if you provide some extra explanation/context (ex what advantage it offers from other answers, what was wrong with the original snippet etc). – Geekfish Apr 23 '19 at 11:15
1

To select To date greater than from date we can use this code

 $(document).on("change", "#from", function () {
        debugger
        var date = $(this).val();
       
        $('#to').attr('min', date);
    
    });
Shivangi
  • 9
  • 1
1

$(function(){
    var dtToday = new Date();
    
    var month = dtToday.getMonth() + 1;
    var day = dtToday.getDate();
    var year = dtToday.getFullYear();
    if(month < 10)
        month = '0' + month.toString();
    if(day < 10)
        day = '0' + day.toString();
    
    var minDate= year + '-' + month + '-' + day;
    
    $('#date').attr('min', minDate);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="date" />
0

If you are using laravel then just do it like this

<input type="date" name="start_date" required min="{{date('Y-m-d')}}" /> 

and for regular HTML just specify the date

<input type="date" name="start_date" required min="2022-07-18" /> 
0

in laravel 5.8,

<input type="date" class="form-control" name="from_date" id="from_date"required min="{{date('Y-m-d')}}"  value="{{ old('from_date') }}">     

                      

here, i used min="{{date('Y-m-d')}}"

vimuth
  • 5,064
  • 33
  • 79
  • 116
ramal
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 18 '22 at 11:46
0

I've found this working (HTML) method. (i.e this works with only textbox with date mode, not with calendars or date-time pickers.)

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script type="text/javascript">
    $(function () {
        var today = new Date();
        var month = ('0' + (today.getMonth() + 1)).slice(-2);
        var day = ('0' + today.getDate()).slice(-2);
        var year = today.getFullYear();
        var date = year + '-' + month + '-' + day;
        $('[id*=txtExpDate]').attr('min', date);
    });
</script>

<asp:TextBox ID="txtExpDate" runat="server" TextMode="Date"></asp:TextBox>
Aruna Prabhath
  • 206
  • 2
  • 10
0

i use something like this i have my input tag

<input name="Date" type="date" onkeyup="checkdate(this)" onchange="checkdate(this)" class="form-control">

and the function is

function checkdate(e){

if(new Date(e.value)-new Date()<0){e.value='';

e.style.border="1px solid red"



return false}
}
Somen Das
  • 366
  • 2
  • 7