How to allow only selection of Sundays on my datepicker? - javascript

How do I allow only selection of Sundays on my datepicker? I did a little bit of research and came across a lot of answers stating that I need to use beforeShowDay, These are my codes:
<div class="pickaDate" style="width:25%;">
<input type='text' id="date" />
<script>
jQuery(document).ready(function() {
jQuery('#date').datepicker({
beforeShowDay: function(date){
return [date.getDay()===0];
}
});
});
</script>
</div>
I got the codes from this link but changed the input type to date because that's the only way the datepicker would appear. But I can still select all the dates.
My knowledge on this is basic at best. Help is appreciated

beforeShowDay: A function that takes a date as a parameter and must return an array with true/false indicating whether or not this date is selectable
Try this:
$("#date").datepicker({
beforeShowDay: function(date) {
return [date.getDay() === 0];
}
});
<link href="http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<input id="date" type="text">
Fiddle here

Related

How to make a two date picker depend on each other in html?

Suppose there are two date pickers. The second date picker will be fully depended on the first one as given in the snippet
$( function() {
$("#txtFromDate").datepicker({
numberOfMonths: 2,
minDate:0,
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected)
}
});
$("#txtToDate").datepicker({
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected)
}
});
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
From: <input type="text" id="txtFromDate" />
To: <input type="text" id="txtToDate" />
If we select a date from the first date picker is there any method if we leave empty the second date picker then the date of second date picker will set to the same date as the first but after the one year. It means if we set the date in first is 07-29-2018 then if we left empty the second date picker then the date will automatically taken as 07-29-2019. Is there any method for doing this. can any body please help me. Thank you.
You set default date for second datepicker on select of first datepicker and vice versa. Here is an example.
$( function() {
$("#txtFromDate").datepicker({
numberOfMonths: 2,
minDate:0,
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected);
$("#txtToDate").datepicker("setDate", '+1y');
}
});
$("#txtToDate").datepicker({
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected)
}
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
From: <input type="text" id="txtFromDate" />
To: <input type="text" id="txtToDate" />

Starting date and ending date are same using Bootstrap datepicker

Starting date and ending date are same. If change starting date automatically changed end date. Here my code
<input id="startingDate" class="datePicker form-control" name="startingDate" value="14-01-2017" type="text">
<input id="endingDate" class="datepicker form-control" name="endingDate" value="" type="text">
Javascript code
$(document).on('blur', '#startingDate', function(){
var date = $(this).val();
$("#endingDate").datetimepicker({
dateFormat: "dd-mm-yy",
defaultDate: date
});
});
What is my problem and how i can solve that problem ??
if you want to have the same value in endingDate when you leave your startingDate
you can do that's
<script>
$(document).ready(function(){
$("#startingDate").blur(function (){
$("#endingDate").val($(this).val());
});
});
</script>

How to populate datepicker's input element with its defaultDate

Is there any way to populate datepicker's input element with its defaultDate?
HTML
<input type="text" id="datepicker">
JS
$("#datepicker").datepicker({
defaultDate: +7
});
https://jsfiddle.net/k9fdw2ub/
I want users to see the default date in the input element itself, not just in the pop-up calendar.
Jsfiddle
Try below code
$("#datepicker").datepicker({
dateFormat: 'dd/mm/yy',
});
$('#datepicker').datepicker('setDate', 'today + 7');
I think this will work for you.
$("#datepicker").datepicker({
defaultDate: +7
});
$('#datepicker').datepicker('setDate', 'today');
// $('#datepicker').datepicker('setDate', 7); If you wanna set date from future 7days from current date use this
By using set setDate you can achieve it.
$("#datepicker").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", new Date());
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input type="text" id="datepicker">

Returning options object for jquery datepicker beforeShow

I want to accept a range of date formats in the input field of a jquery ui datepicker by checking them in the beforeShow function.
According to the docs I need to return an options object, however I can't get this to work.
Here is the minimal code to set the datepicker to 01-10-2015 no matter what is entered:
<input id="test" name="test" value="" />
$('#test').datepicker({
dateFormat: 'dd-mm-yy'
beforeShow: function (input, inst){
return { setDate: '01-10-2015' }
}
});
https://jsfiddle.net/mp4wbuvm/
You're missing a comma after the dateFormat property.
$('#test').datepicker({
dateFormat: 'dd-mm-yy',
beforeShow: function(input, inst) {
return {
setDate: '01-10-2015'
};
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<input id="test" name="test" value="" />

Open two jquery datepickers simultaneously

I have two input fields containing a class that triggers a datepicker to open. Both work fine independently, i.e when I click on one or the other but I want for both datepickers to open when either input field is clicked.
Here is the script part of the code
$(document).ready(function() {
$( ".datefields" ).datepicker({ dateFormat: 'dd/mm/yy',numberOfMonths: 1, yearRange: "2012:2014", changeYear: true, changeMonth: true});
and this is the html
<div id="quoteformcollection">
<h1>Collection</h1>
<input type"text" class="startTbl locationfields" id="AutoLocStart" value="Please Type a Collection Location"onclick="clearVal(this);"/>
<input type="hidden" id="DepotStart" value=""/></td>
<input type="text" class="datefields" id="collectiondate" value="21/05/2012"/>
<input type"text" class="timefields" value="12:00" />
</div>
<div id="quoteformreturn">
<h1>Return</h1>
<input type"text" class="locationfields" value="Enter the city or location for return" />
<input type"text" id="returndate" class="datefields" value="21/05/2012" />
<input type"text" class="timefields" value="12:00" />
</div>
I have tried looking for an answer myself but am quite new to jquery and struggling a bit so any help would be much appreciated.
I would also like for whatever value is selected in the first datepicker, for the default date in the second to be incremented by x number of days, which again I am not sure of the best way to go about it.
Any advice would be hugely appreciated!
Try this code for instance to link the two datapickers:
$('#collectiondate').datepicker({
dateFormat: "#",
onSelect: function(dateText, inst) {
var dateText = eval(dateText);
var min = new Date();
min.setTime(dateText);
$('#end').datepicker('option', 'minDate', min);
}
});
$('#returndate').datepicker({
dateFormat: "#",
});
I think you'd have to use the calendar 'inline' and manage how it's popped up yourself through jQuery / Javascript. You need to point the cal to a div tag to use it inline.
http://jqueryui.com/demos/datepicker/#inline

Categories