How i can add class to every date under selected day.
Lets say i click on Thursday and all dates under Thursday have some class.
I have service where you can chose frequency, weekly, 2 weeks, 4 weeks, and when user click on some date add select frequency the dates are highlighted on the calendar.
I have try to add css to every nth child from ui-state-default but this is not working.
```
$('#cookdate').datepicker({
numberOfMonths: 2,
altField: '#selectedDatecook',
minDate: 2,
maxPicks: 20,
onSelect: function (date) {
var date = $(this).datepicker('getDate');
var day = date.getUTCDay();
},
});
You will want to use a combination of onSelect and beforeShowDate. For example:
$(function() {
var selected, final;
var days = 7;
$('#cookdate').datepicker({
numberOfMonths: 2,
minDate: 2,
onSelect: function(dStr, obj) {
selected = $.datepicker.parseDate("mm/dd/yy", dStr);
final = new Date(selected);
final.setDate(selected.getDate() + days);
$("#selectdate").val($.datepicker.formatDate("mm/dd/yy", final));
},
beforeShowDay: function(dt) {
$(".highlighted").removeClass("highlighted");
if (dt > selected && dt <= final) {
return [true, "highlighted"];
} else {
return [true, ""];
}
}
});
});
td.highlighted, td.highlighted a.ui-state-default {
background-color: #ccc;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>
<p>Cook Date: <input type="text" id="cookdate"> Select Date: <input type="text" id="selectdate"></p>
You can change days to be any number of days you want. beforeShowDays will then look for the matching dates and add a class to them, highlighted.
Related
it looks like the beforeShowDay doesn't work the first time I open the calendar on the current month.
I'm having the jquery ui datepicker with few active days each month.
when I refresh the website and open the calendar for the first time, i'll see that all the dates in January are unactive but if I move to February and go back to Jan i'll see the active days (as I should the first time I opened it).
$("#divDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(value, date) {
//chose date
$("#search_term").val( $("#search_term").val() + value );
$("#divDatePicker").hide();
},
beforeShowDay: function(d) {
var year = d.getFullYear(),
month = ("0" + (d.getMonth() + 1)).slice(-2),
day = ("0" + (d.getDate())).slice(-2);
var formatted = year + '-' + month + '-' + day;
if ($.inArray(formatted, availableDates) != -1) {
return [true, "","Available"];
} else{
return [false,"","unAvailable"];
}
}
});
any ideas?
Consider the following example.
$(function() {
var availableDates = ["23-01-21", "23-01-22"];
$("#divDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(value, date) {
//chose date
var temp = $("#search_term").val();
temp += value;
$("#search_term").val(temp);
$("#divDatePicker").hide();
},
beforeShowDay: function(d) {
var formatted = $.datepicker.formatDate("y-mm-dd", d);
if (availableDates.indexOf(formatted) != -1) {
return [true, "", "Available"];
} else {
return [false, "", "unAvailable"];
}
}
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<div id="divDatePicker"></div>
<input type="text" id="search_term" />
You can use the utility $.datepicker.formatDate( format, date, options ) to format a Date into a String. You stated you wanted YY-MM-DD, so "23-01-05" for today. This must match with some of your availableDates elements, which I am assuming is an array.
I'm trying to disable years from my "YearPicker?", it's a DatePicker where you're only able to pick years. I need to disable all years and enable only a range of years (From 2012 to now, for example).
I'm using the JQueryUI DatePicker and it looks like this:
CSS:
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<style>
.ui-datepicker-calendar {
display: none;
}
</style>
JS:
<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>
$( function() {
$('#datepicker').datepicker({
viewMode: 2,
format: 'yyyy',
maxDate: new Date(new Date().getFullYear(), 11, 31),
minDate: new Date(2012, 0, 1)
});
})
</script>
HTML:
<label>Year: <input class="form-control" type="text" id="datepicker"></label>
You can make what you're doing work, yet I think it's a bit more work than is needed. I would suggest maybe consider using Autocomplete instead. Take a look:
$(function() {
$("#datepicker").autocomplete({
minLength: 0,
source: function(req, resp) {
var today = new Date();
var min = today.getFullYear() - 6;
// Adjust above as needed, currently 6 years before this year
var results = [];
for (min; min <= today.getFullYear(); min++) {
results.push(min.toString());
}
resp(results)
}
}).focus(function(e) {
$(e.target).autocomplete("search", "");
});
})
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.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>
<label>Year: <input class="form-control" type="text" id="datepicker" data-years="6"></label>
Can move this to it's own function if needed too.
var function makeYearList(n){
var today = new Date();
var min = today.getFullYear() - n;
var results = [];
for (min; min <= today.getFullYear(); min++) {
results.push(min.toString());
}
return results;
}
You can also adjust the theming to list them horizontally versus vertically.
Hope that helps.
Solved! I was importing Bootstrap DatePicker and JQueryUI DatePicker, I thought that I was using the second one only, but the app was detecting only the first one, my code now is like this and working:
$('#datepicker').datepicker({
minViewMode: 2,
format: 'yyyy',
startDate: '2012',
endDate: 'y'
});
Thanks all for helping!
You could specify simple strings as well to achieve the desired formatting:
{
format: 'MM/yyyy',
startDate: '01/2022',
endDate: '12/2022',
}
Is there any way to show month input type as number (i.e textbox with up and down arrow) instead of dropdown.
Please help on this. See my demo: https://jsfiddle.net/sharmilashree/VBGKU/1067/
.ui-datepicker-calendar tr {
text-align: center;
padding: 0;
background-color: #F6F6F6
}
This will be a difficult workaround. This is not easily done. Here is what I have so far:
https://jsfiddle.net/Twisty/ge6zh5es/6/
HTML
<p>Date:
<input type="text" id="datepicker" class="Highlighted" />
</p>
JavaScript
$(function() {
var minYear = 2006,
maxYear = new Date().getFullYear();
maxYear++;
var $dp = $("#datepicker").datepicker({
changeYear: true,
dateFormat: "dd-mm-yy",
yearRange: minYear + ':' + maxYear,
});
$dp.datepicker("setDate", "0");
function changeSpinner($dpObj) {
var $yearSpin = $("<input>");
$yearSpin.addClass("ui-datepicker-year");
$yearSpin.attr({
type: "number",
min: minYear,
max: maxYear,
value: new Date().getFullYear()
}).data("handler", "selectYear").data("event", "change");
$yearSpin.on("change", function() {
console.log("Spinner Change Event.");
var pDate = $dpObj.datepicker("getDate");
pDate.setFullYear(parseInt(this.value));
console.log("New Date: " + pDate);
$dpObj.datepicker("setDate", pDate);
});
console.info("Created Spinner: ", $yearSpin);
$dpObj.datepicker("widget").find(".ui-datepicker-year").replaceWith($yearSpin);
}
$("#datepicker").click(function() {
changeSpinner($dp);
}).change(function() {
changeSpinner($db);
});
});
This works the first time only. The datepicker itself does not have any events I can hook into to perform the replacement each time. I also have to add in some functions to get this new element to function properly with the datepicker.
I am considering removing the title bar items and separating them. I will update this answer with more details. Another option is to hide the select element and place a spinner over it. This spinner then changes the select element.
I have jQuery UI datepicker integrated with KnockoutJS.
<input class="input-small hasDatepicker" data-bind="datepicker: ItemCurrentDate, datepickerOptions: { dateFormat: 'dd/mm/yy', changeMonth: true, changeYear: true, minDate: ItemStartDate(), maxDate: ItemEndDate(), datePickerPlaceholder: 'dd/mm/yy' } name="ItemCurrentDate" type="text" value="" placeholder="dd/mm/yy" id="dp12345">
I had to add minDate and maxDate restrictions.
After I added minDate restriction, it set current value(ItemCurrentDate) to maxDate value, even if ItemCurrentDate >= ItemStartDate.
I need ItemStartDate to remain the same when I just open dp.
not totally following the question, but will this work? here is a fiddle. http://jsfiddle.net/LkqTU/33447/ I wasn't sure from your question if min and max dates are observables that can change. I assumed they were static but if not you may need to change the update portion of the custom binding, probably destroy the datepicker and recreate it with the new min and max dates.
you may run the solution below or you may use the fiddle referenced above
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
var options = allBindingsAccessor().datePickeroptions || {};
options.onSelect = function(selected, evnt) {
var observable = valueAccessor();
observable(selected);
};
$(element).datepicker(options);
// setting initial value
$(element).datepicker("setDate", valueAccessor()());
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
//update the control when the view model changes
update: function(element, valueAccessor, allBindingsAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).datepicker("setDate", valueAccessor()());
}
};
function model() {
var self = this;
this.itemCurrentDate = ko.observable(new Date(2017, 1, 26));
this.itemStartDate = ko.observable(new Date(2017, 1, 1));
this.itemEndDate = ko.observable(new Date(2017, 2, 22));
this.resetDate = function() {
self.itemCurrentDate(new Date(2017, 1, 26));
}
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<input data-bind="datepicker: itemCurrentDate,
datePickeroptions: {
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
minDate: itemStartDate(),
maxDate: itemEndDate(),
datePickerPlaceholder: 'dd/mm/yy'
}
" />
<p>
theDate: <span data-bind="text: itemCurrentDate"></span>
</p>
<p>
<input type="button" data-bind="click: resetDate" value="reset date" </p>
</p>
I have two date-pickers #from and #to, as usual, now what I need to do is make sure that the date range selected between the two is always within 3months of each other. So basically, on selecting one of the pickers, I need to set minDate/maxDate option for the other one relative to the selected date, But I don't know what would be the best way to find these relative dates. Any suggestions?
Alternatively you could do it like this:fiddle
Both ways are valid, i suppose.
.js
$("#startDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(dateText,dateObj){
var dateObject = $(this).datepicker("getDate");
dateObject.setMonth(dateObj.currentMonth+3);
$("#endDatePicker").datepicker( "option", "maxDate",dateObject);
}
});
$("#endDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
onSelect: function(dateText,dateObj){
var dateObject = $(this).datepicker("getDate");
dateObject.setMonth(dateObj.currentMonth-3);
$("#startDatePicker").datepicker( "option", "minDate",dateObject);
}
});
I have created a jsfiddle.The range between the days is set to 3 days.you can edit based on ur requirement
$(document).ready(function () {
$("#startDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function (date) {
if ($('#endDatePicker').val() == "") {
var selectedDate = new Date(date);
var secsPerDay = 86400000;
var endDate = new Date(selectedDate.getTime() + 2 * secsPerDay);
alert(endDate);
$("#endDatePicker").datepicker("option", "minDate", selectedDate);
$("#endDatePicker").datepicker("option", "maxDate", endDate);
}
}
});
$("#endDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
onSelect: function (date) {
if ($('#startDatePicker').val() == "") {
var selectedDate = new Date(date);
var secsPerDay = 86400000;
var startDate = new Date(selectedDate.getTime() - 5 * secsPerDay);
$("#startDatePicker").datepicker("option", "minDate", startDate);
$("#startDatePicker").datepicker("option", "maxDate", selectedDate);
}
}
});
});
and HTML
<p>Start Date:
<input type="text" id="startDatePicker"/>
</p>
<p>End Date:
<input type="text" id="endDatePicker"/>
</p>