jQuery Date Picker-wyłącz wcześniejsze daty

Próbuję wybrać zakres dat za pomocą selektora daty UI.

W polu Od/Do ludzie nie powinni być w stanie wyświetlić ani wybrać daty poprzedzającej dzień dzisiejszy.

To jest mój kod:

$(function() {
    var dates = $( "#from, #to" ).datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        onSelect: function( selectedDate ) {
            var option = this.id == "from" ? "minDate" : "maxDate",
                instance = $( this ).data( "datepicker" ),
                date = $.datepicker.parseDate(
                    instance.settings.dateFormat ||
                    $.datepicker._defaults.dateFormat,
                    selectedDate, instance.settings );
            dates.not( this ).datepicker( "option", option, date );
        }
    });
});

Czy ktoś może mi powiedzieć, jak wyłączyć daty poprzednie do obecnej daty.

Author: Harsha M V, 2011-12-02

16 answers

Musisz utworzyć nowy obiekt date i ustawić go jako minDate podczas inicjalizacji datepickers

<label for="from">From</label> <input type="text" id="from" name="from"/> <label for="to">to</label> <input type="text" id="to" name="to"/>

var dateToday = new Date();
var dates = $("#from, #to").datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 3,
    minDate: dateToday,
    onSelect: function(selectedDate) {
        var option = this.id == "from" ? "minDate" : "maxDate",
            instance = $(this).data("datepicker"),
            date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings);
        dates.not(this).datepicker("option", option, date);
    }
});

Edit - od Twojego komentarza teraz działa zgodnie z oczekiwaniami http://jsfiddle.net/nicolapeluchetti/dAyzq/1/

 114
Author: Nicola Peluchetti,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-02 12:59:31

Zadeklaruj zmienną dateToday i użyj funkcji Date (), aby ją ustawić.. następnie użyj tej zmiennej, aby przypisać minDate, który jest parametrem datepicker.

var dateToday = new Date(); 
$(function() {
    $( "#datepicker" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true,
        minDate: dateToday
    });
});
To wszystko... Powyższa odpowiedź była bardzo pomocna... tak trzymać chłopaki..
 74
Author: Sachin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-23 09:50:11
$('#datepicker-dep').datepicker({
    minDate: 0
});

minDate:0 mi pasuje.

 47
Author: Riya Travel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-14 13:51:11

Użyj opcji "minDate", aby ograniczyć najwcześniejszą dozwoloną datę. Wartość " 0 " oznacza dzisiaj (0 dni od dzisiaj):

    $(document).ready(function () {
        $("#txtdate").datepicker({
            minDate: 0,
            // ...
        });
    });

Dokumenty tutaj: http://api.jqueryui.com/datepicker/#option-minDate

 20
Author: Mosin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-27 17:20:07

Żeby dodać do tego:

Jeśli musisz również uniemożliwić użytkownikowi ręczne wpisanie daty w przeszłości, jest to możliwe rozwiązanie. To właśnie zrobiliśmy (na podstawie odpowiedzi @Nicola Peluchetti)

var dateToday = new Date();

$("#myDatePickerInput").change(function () {
    var updatedDate = $(this).val();
    var instance = $(this).data("datepicker");
    var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, updatedDate, instance.settings);

    if (date < dateToday) {
        $(this).datepicker("setDate", dateToday);
    }
});

To, co robi, to zmiana wartości na dzisiejszą datę, jeśli użytkownik ręcznie wpisuje datę w przeszłości.

 8
Author: PålOliver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-20 06:41:36

Live Demo , try this,

    $('#from').datepicker(
     { 
        minDate: 0,
        beforeShow: function() {
        $(this).datepicker('option', 'maxDate', $('#to').val());
      }
   });
  $('#to').datepicker(
     {
        defaultDate: "+1w",
        beforeShow: function() {
        $(this).datepicker('option', 'minDate', $('#from').val());
        if ($('#from').val() === '') $(this).datepicker('option', 'minDate', 0);                             
     }
   });
 6
Author: hari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-11 13:24:28

To jest łatwy sposób, aby to zrobić

$('#datepicker').datepicker('setStartDate', new Date());

Również możemy wyłączyć przyszłe dni

$('#datepicker').datepicker('setEndDate', new Date());
 5
Author: Sameera Prasad Jayasinghe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-29 10:03:10

Ustaw atrybut startDate datepicker, działa, poniżej znajduje się kod roboczy

$(function(){
$('#datepicker').datepicker({
    startDate: '-0m'
    //endDate: '+2d'
}).on('changeDate', function(ev){
    $('#sDate1').text($('#datepicker').data('date'));
    $('#datepicker').datepicker('hide');
});
})
 4
Author: Anna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-24 09:26:54

Przez ustawienie startDate: new Date()

$('.date-picker').datepicker({
    defaultDate: "+1w",
    changeMonth: true,
    numberOfMonths: 1,
    ...
    startDate: new Date(),
});
 3
Author: kheengz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-07 10:42:44

JQuery API documentation-datepicker

Minimalna Data do wyboru. Po ustawieniu na null, nie ma minimum.

Obsługa wielu typów:

Date: obiekt date zawierający datę minimalną.
Liczba: liczba dni od dzisiaj. Na przykład 2 reprezentuje two days od dzisiaj, a -1 reprezentuje yesterday.
String: łańcuch w formacie zdefiniowanym przez opcję dateFormat, lub względny Data.
Daty względne muszą zawierać pary wartości i okresu; ważne okresy to y dla years, m na months, w dla weeks i d dla days. Na przykład, +1m +7d reprezentuje one month and seven days z today.

Aby nie wyświetlać poprzednich dat innych niż dzisiaj

$('#datepicker').datepicker({minDate: 0});
 2
Author: Mohammad Faisal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-16 11:51:51

"mindate " atrybut powinien być użyty do wyłączenia przekazywanych dat w jQuery datepicker.

MinDate: new Date () lub minDate: '0' jest kluczem do tego.

Ex:

$(function() {
    $( "#datepicker" ).datepicker({minDate: new Date()});
});

Lub

$(function() {
  $( "#datepicker" ).datepicker({minDate: 0});
});
 2
Author: sreekanth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-21 11:45:23

Po prostu Zamień swój kod:

Stary kod:

$("#dateSelect").datepicker({
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd'

});

Nowy kod:

$("#dateSelect").datepicker({
    showOtherMonths: true,
    selectOtherMonths: true,
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: 'yy-mm-dd',
    minDate: 0
});
 2
Author: user5663780,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-10 10:59:26

Stworzyłem funkcję wyłączającą poprzednią datę, wyłączającą elastyczne dni weekendowe (np. Sobota, Niedziela)

Używamy beforeShowDay Metody wtyczki jQuery UI datepicker.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var NotBeforeToday = function(date) {
		var now = new Date(); //this gets the current date and time
		if (date.getFullYear() == now.getFullYear() && date.getMonth() == now.getMonth() && date.getDate() >= now.getDate() && (date.getDay() > 0 && date.getDay() < 6) )
			return [true,""];
		if (date.getFullYear() >= now.getFullYear() && date.getMonth() > now.getMonth() && (date.getDay() > 0 && date.getDay() < 6))
			return [true,""];
		if (date.getFullYear() > now.getFullYear() && (date.getDay() > 0 && date.getDay() < 6))
			return [true,""];
		return [false,""];
	}


jQuery("#datepicker").datepicker({
		beforeShowDay: NotBeforeToday
    });

Tutaj wpisz opis obrazka

Oto dzisiejsza data to 15 września. Mam wyłączone sobota i niedziela.

 1
Author: Smit Patadiya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-15 07:56:10

Możesz po prostu użyć

startDate: 'today'
Dla mnie działa dobrze.
 1
Author: Moaz Ateeq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-09-03 09:04:55

Musisz zadeklarować bieżącą datę do zmiennych takich jak Ta

 $(function() {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentDate = date.getDate();
    var currentYear = date.getFullYear();
    $('#datepicker').datepicker({
    minDate: new Date(currentYear, currentMonth, currentDate)
    });
})
 0
Author: hoga98,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-09-27 00:26:47
$( "#date" ).datetimepicker({startDate:new Date()}).datetimepicker('update', new Date());

new Date() : funkcja get the todays date poprzednia Data jest zablokowana. 100% pracy

 0
Author: Muhammad Usman Nasir,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-03 06:29:21