I have been trying to implement some additional code to disable a range of dates in the future on my date range picker, from my research I believe I need to use beforeShowDay, have the dates in an array and return a false if the dates are in the array but each time I try to spanner in some code it breaks the pickers. I have to confess I don't know Javascript very well at all and only use it with plugins when I have to, any help would be gratefully received.
var dateToday = new Date();
var dates = $("#from, #to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
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);
}
});
I would like to disable from the 2nd of December 2017 to the January 27th 2018
$('#datePick').datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 1,
minDate: new Date(),
beforeShowDay: function(date){
var val = new Date("2017-12-02") >= date || new Date("2018-01-28") < date ;
return [ val ]
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/redmond/jquery-ui.css" rel="stylesheet"/>
// disabled 2nd of December 2017 to January 27th 2018<br>
<input id='datePick'>
use beforeShowDay and check which date you need to show.
Related
i need dates in datepicker like below pattern using Zig-Zag flow in Javascript Datepicker
2014-11-10 - 2014-11-15
2014-11-16 - 2014-11-17
2014-11-18 - 2014-11-18
As you haven't specified any specific JS-Library, I'm assuming you're referring to the HTML5 Standard <input type="date">, which renders a datepicker depending on the browser currently used.
The format of this datepicker shown to the user is determined by the locale of the users browser, whereas the value retrieved using JavaScript is always in the format yyyy-mm-dd.
const dateNode = document.querySelector("input[type=date]")
dateNode.addEventListener('change', (event) => {
console.log(`Current JS-Value: ${event.target.value}, which is of type ${typeof event.target.value}`)
})
<label>Enter date:
<input type="date">
</label>
Finally i worked around on this soltuion.
function remove(tr_id){
$("#tr_"+tr_id).remove();
counter = $("#counter").val();
counter = parseInt( counter ) - 1 ;
$("#counter").val(counter);
}
function AddNewElement(){
counter = $("#counter").val();
counter = parseInt( counter ) + 1 ;
// YOUR SPECIFIC DYNAMIC HTML
var elestr = '<tr id="tr_'+counter+'" class="ele_tr"><td><input onclick="remove('+counter+');" id="remove" type="button" value="Remove"></td></tr>';
$("#ele_list").append(elestr);
$("#counter").val(counter);
}
$('body').on('focus',".dp_start", function(){
counter = $(this).data('counter');
counter = parseInt(counter);
var trialDate = new Date ($("#enddate_"+ (counter-1)).val());
trialDate.setDate ( trialDate.getDate () + 1 );
$(this).datepicker({
// changeMonth: true, changeYear: true,
dateFormat: "yy-mm-dd",
minDate: trialDate,
onClose: function (selectedDate) {
$("#enddate_"+counter).datepicker("option", "minDate", selectedDate);
}
});
});
$('body').on('focus',".dp_end", function(){
counter = $(this).data('counter');
counter = parseInt(counter);
$(this).datepicker({
// changeMonth: true, changeYear: true,
dateFormat: "yy-mm-dd",
minDate: $("#startdate_"+ (counter)).val(),
onClose: function (selectedDate) {
$("#startdate_"+counter).datepicker("option", "maxDate", selectedDate);
}
});
});
Two datepickers, staff are to enter a weekday into datepicker 1 (#from) and then enter in a second date into (#to). Date 2 must always be in the future with respect to date 1. Weekends not allowed.
working example of this, excluding weekends, and not allowing past dates in date 2, is available in this fiddle:
https://jsfiddle.net/gLrumpo3/6/
<input id="from">
<input id="to">
$("#from").datepicker({
defaultDate: new Date(),
minDate: new Date(),
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(dateStr)
{
$("#to").val(dateStr);
$("#to").datepicker("option",{ minDate: new Date(dateStr)})
}
});
$('#to').datepicker({
defaultDate: new Date(),
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(dateStr) {
toDate = new Date(dateStr);
fromDate = ConvertDateToShortDateString(fromDate);
toDate = ConvertDateToShortDateString(toDate);
}
});
Waht I need now though, is to be able to "lock" the 2nd date input to the same WEEK as the first date, I thought about ising maxDate, but I can only specifiy an arbitrary offset, like Xdays, or a specific date. This is no good as people may select thursday for example, i cant then add 4 days to it and set it as max date, as they would be able to select monday next week.
So how can i constrain their date inouts to one week, where that week is set absed on the day selected of the 1st field.
Please update fiddle with working answers if possible. Thanks.
Try this code
var date = $(this).datepicker('getDate');
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
$( "#to" ).datepicker("option",{minDate: new Date(dateStr), maxDate:endDate});
You can set maxDate to last date of that week for $( "#to" ) datepicker.
$("#from").datepicker({
defaultDate: new Date(),
minDate: new Date(),
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(dateStr)
{
$("#to").val(dateStr);
var date = $(this).datepicker('getDate');
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
$( "#to" ).datepicker("option",{minDate: new Date(dateStr), maxDate:endDate});
}
});
$('#to').datepicker({
defaultDate: new Date(),
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(dateStr) {
toDate = new Date(dateStr);
//fromDate = ConvertDateToShortDateString(fromDate);
//toDate = ConvertDateToShortDateString(toDate);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<input id="from">
<input id="to">
Which one is correct. And what exactly difference between?
$(function() {
var today = new Date();
today.setDate(today.getDate());
$('#date').datetimepicker({
pickTime: false,
minDate: today
});
}
and
$(function() {
var today = new Date();
today.setDate(today.getDate());
$('#date').datetimepicker({
pickTime: false,
startDate: today
});
}
You are not allowed to select date before minDate. Startdate is just an initial value.
in jquery-datetimepicker-2.5.21,
You are not allowed to select a date before startDate and minDate does not work
I am trying to set default date for Jquery datepicker on init. Here is my html:
var content = "";
content = "<table class=\"filter-table\">";
content += "<tr><td><label for='startDate'>From </label></td><td><input name='startDate' id='startDate' class='date-picker' /></td></tr>";
content += "<tr><td> </td></tr>";
content += "<tr><td><label for='endDate'>To </label></td><td><input name='endDate' id='endDate' class='date-picker' /></td></tr>";
And my JavaScript:
<script type="text/javascript">
function showdatepicker() {
$('.date-picker').datepicker( {
changeMonth: true,
changeYear: true,
changeDay: true,
showButtonPanel: true,
dateFormat: 'yy-mm-dd',
onClose: function(dateText, inst) {
var day = $("#ui-datepicker-div .ui-datepicker-day :selected").val();
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$('#startDate').datepicker({defaultDate: -30});
$('#endDate').datepicker({defaultDate: new Date()});
}
});
}
</script>
What I am trying to do is set the date and show in the textbox when on load the page. The endDate should be current date whilst start date should be one month before. However, by using these codes, it does not display the date in textbox when first load.
I wonder why is it so. Thanks in advance.
I, like you, could not use a method on the datepicker to populate a default date in the input.
So instead, I made code using vanilla JS to calculate the default dates and format them to display in the textboxes.
Here is the JS I added:
function showdatepicker() {
// same as yours
}
function populateDefaultValues() {
var today = new Date();
var month = today.getMonth() - 1,
year = today.getFullYear();
if (month < 0) {
month = 11;
year -= 1;
}
var oneMonthAgo = new Date(year, month, today.getDate());
$('#startDate').val($.datepicker.formatDate('yy-mm-dd', today));
$('#endDate').val($.datepicker.formatDate('yy-mm-dd', oneMonthAgo));
}
$(function() {
populateDefaultValues();
showdatepicker();
});
jsFiddle
Have a look at the defaultDate option. It provides exactly the features you need.
Because the default date options will vary for each datepicker you will need to initialise them separately (basically copy-paste, and tweak to suit).
http://api.jqueryui.com/datepicker/#option-defaultDate
I need to use datepicker which provides me the option of restricting the selectable dates. We had been using jQuery UI which is used to support it using minDate, maxDate options.
$("#id_date").datepicker({minDate: +1, maxDate: '+1M +10D'});
Recently we started using Twitter Bootstrap for our styles. And apparently, Twitter Bootstrap is incompatible with jQuery UI styles. So I tried using one of the bootstrap compatible datepickers available at http://www.eyecon.ro/bootstrap-datepicker/.
Unfortunately, the above plugin is not as configurable as jQuery UI's datepicker. Can anyone help me out with restricting the selectable date ranges in the new datepicker.
The Bootstrap datepicker is able to set date-range. But it is not available in the initial release/Master Branch. Check the branch as 'range' there (or just see at https://github.com/eternicode/bootstrap-datepicker), you can do it simply with startDate and endDate.
Example:
$('#datepicker').datepicker({
startDate: '-2m',
endDate: '+2d'
});
With selectable date ranges you might want to use something like this. My solution prevents selecting #from_date bigger than #to_date and changes #to_date startDate every time when user selects new date in #from_date box:
http://bootply.com/74352
JS file:
var startDate = new Date('01/01/2012');
var FromEndDate = new Date();
var ToEndDate = new Date();
ToEndDate.setDate(ToEndDate.getDate()+365);
$('.from_date').datepicker({
weekStart: 1,
startDate: '01/01/2012',
endDate: FromEndDate,
autoclose: true
})
.on('changeDate', function(selected){
startDate = new Date(selected.date.valueOf());
startDate.setDate(startDate.getDate(new Date(selected.date.valueOf())));
$('.to_date').datepicker('setStartDate', startDate);
});
$('.to_date')
.datepicker({
weekStart: 1,
startDate: startDate,
endDate: ToEndDate,
autoclose: true
})
.on('changeDate', function(selected){
FromEndDate = new Date(selected.date.valueOf());
FromEndDate.setDate(FromEndDate.getDate(new Date(selected.date.valueOf())));
$('.from_date').datepicker('setEndDate', FromEndDate);
});
HTML:
<input class="from_date" placeholder="Select start date" contenteditable="false" type="text">
<input class="to_date" placeholder="Select end date" contenteditable="false" type="text"
And do not forget to include bootstrap datepicker.js and .css files aswell.
The example above can be simplify a bit. Additionally you can put date manually from keyboard instead of selecting it via datepicker only. When clearing the value you need to handle also 'on clearDate' action to remove startDate/endDate boundary:
JS file:
$(".from_date").datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
}).on('changeDate', function (selected) {
var startDate = new Date(selected.date.valueOf());
$('.to_date').datepicker('setStartDate', startDate);
}).on('clearDate', function (selected) {
$('.to_date').datepicker('setStartDate', null);
});
$(".to_date").datepicker({
format: 'yyyy-mm-dd',
autoclose: true,
}).on('changeDate', function (selected) {
var endDate = new Date(selected.date.valueOf());
$('.from_date').datepicker('setEndDate', endDate);
}).on('clearDate', function (selected) {
$('.from_date').datepicker('setEndDate', null);
});
HTML:
<input class="from_date" placeholder="Select start date" type="text" name="from_date">
<input class="to_date" placeholder="Select end date" type="text" name="to_date">
Most answers and explanations are not to explain what is a valid string of endDate or startDate.
Danny gave us two useful example.
$('#datepicker').datepicker({
startDate: '-2m',
endDate: '+2d'
});
But why?let's take a look at the source code at bootstrap-datetimepicker.js.
There are some code begin line 1343 tell us how does it work.
if (/^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$/.test(date)) {
var part_re = /([-+]\d+)([dmwy])/,
parts = date.match(/([-+]\d+)([dmwy])/g),
part, dir;
date = new Date();
for (var i = 0; i < parts.length; i++) {
part = part_re.exec(parts[i]);
dir = parseInt(part[1]);
switch (part[2]) {
case 'd':
date.setUTCDate(date.getUTCDate() + dir);
break;
case 'm':
date = Datetimepicker.prototype.moveMonth.call(Datetimepicker.prototype, date, dir);
break;
case 'w':
date.setUTCDate(date.getUTCDate() + dir * 7);
break;
case 'y':
date = Datetimepicker.prototype.moveYear.call(Datetimepicker.prototype, date, dir);
break;
}
}
return UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds(), 0);
}
There are four kinds of expressions.
w means week
m means month
y means year
d means day
Look at the regular expression ^[-+]\d+[dmwy]([\s,]+[-+]\d+[dmwy])*$.
You can do more than these -0d or +1m.
Try harder like startDate:'+1y,-2m,+0d,-1w'.And the separator , could be one of [\f\n\r\t\v,]
Another possibility is to use the options with data attributes, like this(minimum date 1 week before):
<input class='datepicker' data-date-start-date="-1w">
More info: http://bootstrap-datepicker.readthedocs.io/en/latest/options.html
i am using v3.1.3 and i had to use data('DateTimePicker') like this
var fromE = $( "#" + fromInput );
var toE = $( "#" + toInput );
$('.form-datepicker').datetimepicker(dtOpts);
$('.form-datepicker').on('change', function(e){
var isTo = $(this).attr('name') === 'to';
$( "#" + ( isTo ? fromInput : toInput ) )
.data('DateTimePicker')[ isTo ? 'setMaxDate' : 'setMinDate' ](moment($(this).val(), 'DD/MM/YYYY'))
});
<script type="text/javascript">
$(document).ready( function() {
$("#submitdate").datepicker( {
minDate: 0,
maxDate: 0,
dateFormat: "yy-mm-dd",
});