jQuery 날짜 선택기-과거 날짜 비활성화
UI 날짜 선택기를 사용하여 날짜 범위를 선택하려고합니다.
시작 / 끝 필드에서 사람들은 현재 날짜 이전의 날짜를 보거나 선택할 수 없어야합니다.
이것은 내 코드입니다.
$(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 );
}
});
});
누군가가 현재 날짜 이전의 날짜를 비활성화하는 방법을 알려줄 수 있습니까?
새 날짜 개체를 만들고 날짜 minDate
선택기를 초기화 할 때 와 같이 설정해야합니다.
<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);
}
});
편집-이제 귀하의 의견에서 예상대로 작동합니다. http://jsfiddle.net/nicolapeluchetti/dAyzq/1/
dateToday 변수를 선언하고 Date () 함수를 사용하여 설정합니다. 그런 다음 해당 변수를 사용하여 datepicker의 매개 변수 인 minDate에 할당합니다.
var dateToday = new Date();
$(function() {
$( "#datepicker" ).datepicker({
numberOfMonths: 3,
showButtonPanel: true,
minDate: dateToday
});
});
그게 다야 ... 위의 답변은 정말 도움이되었다 ... 계속해서 얘들 아 ..
$('#datepicker-dep').datepicker({
minDate: 0
});
minDate:0
나를 위해 작동합니다.
허용되는 가장 이른 날짜를 제한하려면 "minDate"옵션을 사용하십시오. 값 "0"은 오늘 (오늘부터 0 일)을 의미합니다.
$(document).ready(function () {
$("#txtdate").datepicker({
minDate: 0,
// ...
});
});
여기 문서 : http://api.jqueryui.com/datepicker/#option-minDate
이것에 추가하기 :
또한 사용자가 과거 날짜 를 수동으로 입력하지 못하도록 해야하는 경우 가능한 해결 방법입니다. 이것이 우리가 한 일입니다 (@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);
}
});
사용자가 수동으로 과거 날짜를 입력하면 값을 오늘 날짜로 변경합니다.
라이브 데모 , 시도해보십시오.
$('#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);
}
});
이것은 이것을하는 쉬운 방법입니다
$('#datepicker').datepicker('setStartDate', new Date());
또한 미래의 날을 비활성화 할 수 있습니다.
$('#datepicker').datepicker('setEndDate', new Date());
datepicker의 startDate 속성을 설정하면 작동합니다. 아래는 작동 코드입니다.
$(function(){
$('#datepicker').datepicker({
startDate: '-0m'
//endDate: '+2d'
}).on('changeDate', function(ev){
$('#sDate1').text($('#datepicker').data('date'));
$('#datepicker').datepicker('hide');
});
})
선택 가능한 최소 날짜입니다. 로 설정하면 null
최소값이 없습니다.
Multiple types supported:
Date: A date object containing the minimum date.
Number: A number of days from today. For example 2
represents two days
from today and -1
represents yesterday
.
String: A string in the format defined by the dateFormat
option, or a relative date.
Relative dates must contain value and period pairs; valid periods are y
for years
, m
for months
, w
for weeks
, and d
for days
. For example, +1m +7d
represents one month and seven days
from today
.
In order not to display previous dates other than today
$('#datepicker').datepicker({minDate: 0});
"mindate" attribute should be used to disable passed dates in jquery datepicker.
minDate: new Date() Or minDate: '0' is the key for this.
Ex:
$(function() {
$( "#datepicker" ).datepicker({minDate: new Date()});
});
OR
$(function() {
$( "#datepicker" ).datepicker({minDate: 0});
});
just replace your code:
old code:
$("#dateSelect").datepicker({
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'yy-mm-dd'
});
new code:
$("#dateSelect").datepicker({
showOtherMonths: true,
selectOtherMonths: true,
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'yy-mm-dd',
minDate: 0
});
By setting startDate: new Date()
$('.date-picker').datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
...
startDate: new Date(),
});
you have to declare current date into variables like this
$(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)
});
})
$( "#date" ).datetimepicker({startDate:new Date()}).datetimepicker('update', new Date());
new Date()
: function get the todays date previous date are locked. 100% working
I have created function to disable previous date, disable flexible weekend days (Like Saturday, Sunday)
We are using beforeShowDay method of jQuery UI datepicker plugin.
<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
});
Here today's date is 15th Sept. I have disabled Saturday and Sunday.
참고URL : https://stackoverflow.com/questions/8356358/jquery-date-picker-disable-past-dates
'program story' 카테고리의 다른 글
PendingIntent는 첫 번째 알림에 대해서는 올바르게 작동하지만 나머지 알림에는 올바르지 않습니다. (0) | 2020.09.15 |
---|---|
문자열에서 부동 숫자를 추출하는 방법 (0) | 2020.09.15 |
Google지도 API v2의 기본 위치 및 확대 / 축소 수준을 어떻게 설정합니까? (0) | 2020.09.15 |
Django 인기의 역사 (0) | 2020.09.15 |
PowerShell의 파일 이름에서 경로 및 확장자 제거 (0) | 2020.09.15 |