Disable future dates after today in Jquery Ui Datepicker - javascript

I want to disable all the future dates after today in Jquery Ui Datepicker
Here is the Demo :
Code :
$( "#start_date" ).datepicker(
{
maxDate: '0',
beforeShow : function()
{
jQuery( this ).datepicker('option','maxDate', jQuery('#end_date').val() );
},
altFormat: "dd/mm/yy",
dateFormat: 'dd/mm/yy'
}
);
$( "#end_date" ).datepicker(
{
maxDate: '0',
beforeShow : function()
{
jQuery( this ).datepicker('option','minDate', jQuery('#start_date').val() );
} ,
altFormat: "dd/mm/yy",
dateFormat: 'dd/mm/yy'
}
);

Try this
$(function() {
$( "#datepicker" ).datepicker({ maxDate: new Date() });
});
Or you can achieve this using as below:
$(function() {
$( "#datepicker" ).datepicker({ maxDate: 0 });
});
Reference
DEMO
UPDATED ANSWER

This worked for me endDate: "today"
$('#datepicker').datepicker({
format: "dd/mm/yyyy",
autoclose: true,
orientation: "top",
endDate: "today"
});
SOURCE

In my case, I have given this attribute to the input tag
data-date-start-date="0d"
data-date-end-date="0d"

You can simply do this
$(function() {
$( "#datepicker" ).datepicker({ maxDate: new Date });
});
JSFiddle
FYI: while checking the documentation, found that it also accepts numeric values too.
Number: A number of days from today. For example 2 represents two days
from today and -1 represents yesterday.
so 0 represents today. Therefore you can do this too
$( "#datepicker" ).datepicker({ maxDate: 0 });

Change maxDate to current date
maxDate: new Date()
It will set current date as maximum value.

In case you are appending Dtpicker,use the following code
$('#enddate').appendDtpicker({
"dateOnly": true,
"dateFormat": "YYYY-MM-DD",
"closeOnSelected": true,
maxDate: new Date()
});

//Disable future dates after current date
$("#datepicker").datepicker('setEndDate', new Date());
//Disable past dates after current date
$("#datepicker").datepicker('setEndDate', new Date());

Datepicker does not have a maxDate as an option. I used this endDate option. It worked well.
$('.demo-calendar-default').datepicker({
autoHide: true,
zIndex: 2048,
format: 'dd/mm/yyyy',
endDate: new Date()
});

maxDate: new Date()
its working fine for me disable with current date in date range picker

Related

How may I return a date in format YYYYMMDD using JQuery DatePicker?

I'm trying to implement the JQuery DatePicker w/ week no and returning the selected date in format YYYYMMDD. When looking into the api for how to format date it says this could be done by using dateFormat: "yy-mm-dd". However, I'm not able to return the date in specified format.
I'm able to return the date in 'original format' with this code:
$(function DatePicker() {
$( "#datepicker" ).datepicker({
onSelect: function(date) {
var date = $(this).datepicker( 'getDate' );
alert(date);
( 'getDate' ).getDate();
},
showWeek: true,
//showOn:"button",
firstDay: 1
});
});
Here is my code for trying to return (show a pop up) wewith the format 'YYYYMMDD':
$(function DatePicker() {
$( "#datepicker" ).datepicker({
// dateFormat: 'yy-mm-dd',
onSelect: function() {
dateFormat: 'yy-mm-dd',
//var dateFormat = $(this).datepicker( "option", "dateFormat" );
//$.(this).datepicker("option", "dateFormat", "yy-mm-dd"); //Senast insatt
alert(dateFormat);
},
showWeek: true,
firstDay: 1
});
});
Obviously I'm new to JQuery, and fairly new to JavaScript as a language.
Thanks!
Check this:
$( "#datepicker" ).datepicker({
dateFormat: 'yymmdd',
onSelect: function(v){
alert(v);
},
showWeek: true,
firstDay: 1
});
Test code here: http://codepen.io/anon/pen/yOBwLo

select by default previous date with jquery calendar

I have datepicker codes below to display me calendar, the current date is selected by default, i would like to select the previous date by default instead (the day before today). I have tried but i failed anybody can help me please?
$(function() {
$("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val()
});
I finally resolved my issue this way
$(function() {
$("#datepicker").datepicker({ dateFormat: "yy-mm-dd" }).val();
$("#datepicker").click(function() {
date = new Date();
date.setDate(date.getDate()-1);
$( "#datepicker" ).datepicker( "setDate" , date);
});
});
Try this:
$(".datepicker").datepicker("setDate", -1)
More info: http://api.jqueryui.com/datepicker/#method-setDate

How to disable already selected date in jquery ui in second date input textbox

i am trying to get multiple date inputs. What i want to do is that when user selects first date then he should not be able to select the same date + the dates previous then selected date in the second text box. here is my code.i am new to this. Not good at jquery ui
<script>
$(function() {//alert("getPricing just got called");
$( "#datepicker" ).datepicker({beforeShowDay: $.datepicker.noWeekends, dateFormat: 'yy-mm-dd',minDate: 0 });
//Date d1=new Date($("#datepicker").val());
$( "#datepicker1" ).datepicker({beforeShowDay: $.datepicker.noWeekends, dateFormat: 'yy-mm-dd',minDate: 0});
});
$("#button").submit(function() {alert("getPricing just got called");
var start = $('#datepicker').datepicker('getDate');
var end = $('#datepicker1').datepicker('getDate');
var days = (end - start)/1000/60/60/24;
alert(days);
});
</script>
now what i want is that user can not select the same date+previous dates (which is selected in #datepicker) at #datepicker1
Demo,try this,
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends,
dateFormat: 'yy-mm-dd',
beforeShow: function() {
$('.datepicker').datepicker('option', 'maxDate', $('#end_date').val());
}
});
$( "#datepicker1" ).datepicker({
beforeShowDay: $.datepicker.noWeekends,
dateFormat: 'yy-mm-dd',
beforeShow: function() {
$(this).datepicker('option', 'minDate', $('#datepicker').val());
}
});

Format date of JQuery Datepicker [duplicate]

I use the datepicker from jQuery UI, but the format is like example today: 08/01/2013
But i want the format to be example today: 2013-08-01
Here is code line i think maybe i should use?
`$( "#txtFromDate" ).datepicker( "option", "dateFormat", "yyyy-mm-dd" );`
And here is my jQuery code i use that works fine but don't know where to put the line?
And i want the format in both of my fields.
`
$(document).ready(function(){
$("#txtFromDate").datepicker({
minDate: "07/17/2012",
maxDate: "0D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected);
}
});
$("#txtToDate").datepicker({
minDate: "07/17/2013",
maxDate:"0D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected);
}
});
});`
I hope somebody have a solution for this :-)
since your already have other options in the datepicker... add it there.
try this
$("#txtFromDate").datepicker({
minDate: "07/17/2012",
maxDate: "0D",
numberOfMonths: 2,
dateFormat: "yy-mm-dd", //<----here
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected);
}
});
You can directly set the date format from jquery-ui library,that's work for me.hope for you also
in
function Datepicker()
{
//other code
dateFormat: "yy-mm-dd", // See format options on parseDate
//other code
}

datepicker onClose issue

From researching on this site, I found a way to use 2 datepickers. If you pick a date on the first one, the second one will automatically show with the selected date as new minDate. You can see this in action on my test server here: http://www.zinius.nl/trips
The issue I'm having occurs in this situation:
Select the Check-in-date field, but don’t select a date and click somewhere else. You won’t be
able to get the Check-in-date field datepicker pop-up back up again until you refresh the entire page or first select the Check-out-date.
This is the code I am using:
$( "#datepicker1" ).datepicker({
showOn: 'both',
buttonImage: 'images/ico/calendar.png',
buttonImageOnly: true,
minDate: 0,
firstDay: 0,
dateFormat: 'mm-dd-yy',
changeMonth: false,
numberOfMonths: 1,
onClose: function( selectedDate ) {
var minDate = $(this).datepicker('getDate');
var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
$( "#datepicker2" ).datepicker( "option", "minDate", newMin );
$( "#datepicker2" ).datepicker( "show" );
}
});
$( "#datepicker2" ).datepicker({
showOn: 'both',
buttonImage: 'images/ico/calendar.png',
buttonImageOnly: true,
minDate: '+1d',
changeMonth: false,
firstDay: 0,
dateFormat: 'mm-dd-yy',
numberOfMonths: 1,
onClose: function( selectedDate ) {
}
});
I tried replacing the onClose with onSelect. This solves the issue, but when I select a date on datepicker1, datepicker2 appears and disappears in a flash.
Does anyone have a solution to get this working properly?
Thank you,
Guilliano
Try to change your onClose function on the first datePicker with
...,
onClose: function( selectedDate ) {
var minDate = $(this).datepicker('getDate');
if(minDate){
var newMin = new Date(minDate.setDate(minDate.getDate() + 1));
$( "#datepicker2" ).datepicker( "option", "minDate", newMin );
$( "#datepicker2" ).datepicker( "show" );
}
}
Looking at the console, I can see this error being logged:
Cannot call method 'getDate' of null
It means your statement:
var minDate = $(this).datepicker('getDate');
is causing the problem. What you can do is to first check if the selectedDate (passed to the onClose callback) is valid or not, before attempting to getDate on it.
onClose: function(selectedDate) {
if (selectedDate) {
...
}
}

Categories

Resources