简体   繁体   中英

How do I restrict past dates in html5 input type Date

I am trying to restrict past dates in input type="date" .I can able to restrict future dates,But I don't have idea about restricting 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" />

Any suggestion?

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" />

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

Hope below code will 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 working link

以编程方式,您可以执行以下操作来禁用过去的日期:

<input type='date' min={new Date().toISOString().split('T')[0]} />

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); }
  $(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);
          });
        });``
<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);

I am trying to restrict past dates in input type="date" .I can able to restrict future dates,But I don't have idea about restricting 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" />

Any suggestion?

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); });

 $(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" />

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" /> 

in laravel 5.8,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM