set minDate and maxDate using semantic-ui-calender - javascript

Anyone already used semantic ui calendar?
I got stuck, how can i set the min and max time to 11:00 PM - 6:00 AM.
let today = new Date();
let set_date = new Date(today.getFullYear(), today.getMonth(), today.getDate());
let set_next_date = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
let min = new Date(set_date.setHours(23,0,0,0));
let max = new Date(set_next_date.setHours(6,0,0,0));
startTime.calendar({
type: 'time',
minDate: min,
maxDate: max,
onChange(date, text, mode){
self.updateStartTime(text)
},
})
endTime.calendar({
type: 'time',
minDate: min,
maxDate: max,
startCalendar: startTime,
onChange(date, text, mode){
self.updateEndTime(text)
}
})

You don't need to create date instances to set hours for them like what you did, just do like me and you will have what you wanted.
Define 2 new variables and use setHours method to change their hours:
var minDate = new Date();
var maxDate = new Date();
minDate.setHours(6);
maxDate.setHours(23);
and initialize your Semantic-UI Calendar like below:
startTime.calendar({
type: 'time',
minDate: minDate,
maxDate: maxDate,
onChange(date, text, mode){
self.updateStartTime(text)
},
})
endTime.calendar({
type: 'time',
minDate: minDate,
maxDate: maxDate,
startCalendar: startTime,
onChange(date, text, mode){
self.updateEndTime(text)
}
})
I also provided you an example on JSFiddle:
http://jsfiddle.net/ali_bahrami/ka52oo7s/1/

Related

JQuery: How to set datepicker starting date in range

I am trying to set the start and end date of a date range of a datepicker(), where the starting date of the range is 7 days in the past and the ending date is yesterday.
Currently I have it working so that the starting date of the range is 7 days in the past however I don't know how to set the ending date to yesterday.
I have tried the following, but it doesn't work as I expect it to:
var mindate = new Date();
mindate.setDate(mindate.getDate() - 7);
$('#date').datepicker({
language: 'en',
range : true,
minDate : mindate,
maxDate : new Date() - 1, //I guess the problem is here
multipleDates: true,
multipleDatesSeparator: " - "
})
One approach would be to compute a maxdate using the same technquie as you have used to compute mindate and then apply that max date value to the maxData parameter of the datepicker() instance:
var mindate = new Date();
mindate.setDate(mindate.getDate() - 7);
/* Compute max date of "yesterday" using same method as
min date */
var maxdate = new Date();
maxdate.setDate(maxdate.getDate() - 1);
$('#date').datepicker({
language: 'en',
range : true,
minDate : mindate,
maxDate : maxdate, /* Apply max date to date picker */
multipleDates: true,
multipleDatesSeparator: " - "
});
The datepicker() plugin also allows you to specify minDate and maxDate via the number of days relative to today's date. That means you can achieve the same result as shown above without needing to calculate mindate and maxdate by specifying minDate and maxDate as shown:
$('#date').datepicker({
language: 'en',
range : true,
minDate : -7, /* 7 days ago */
maxDate : -1, /* Yesterday */
multipleDates: true,
multipleDatesSeparator: " - "
});
minDate and maxDate can the amount of days from today.
https://api.jqueryui.com/datepicker/#option-minDate
So all you need to do is
minDate: -1,
maxDate: -7,
In HTML5 you already have a <input type="date"> so you don't need any kind of jQuery.
Unless you need support for legacy browsers I can only recommend using HTML5 over jquery.
HTML
<input type="date" id="date">
Vanilla JS
function getHtmlDateString(date) {
var dd = date.getDate();
var mm = date.getMonth()+1; //January is 0!
var yyyy = date.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
return yyyy+'-'+mm+'-'+dd;
}
var min = new Date();
min.setDate(min.getDate()-7);
document.getElementById("date").setAttribute("min", getHtmlDateString(min));
var max = new Date();
max.setDate(max.getDate()-1);
document.getElementById("date").setAttribute("max", getHtmlDateString(max));
JSFiddle: https://jsfiddle.net/hmqe4sb6/

Bootstrap datetimepicker showing two calendar months

Here it looks like:
I want it to just show one month with it ranging the dates from the minDate to maxDate. And this works good but it is showing two views for some reason..
Here is the JS:
var maxDate = new Date();
var minDate = $("#from").val();
var datePickerLimits= $("#from, #to").datetimepicker({
minDate: minDate,
maxDate: maxDate,
pickTime: false,
onSelect: function(selectedDate) {
var option = this.id == "from" ? "minDate" : "maxDate",
instance = $(this).data("datetimepicker"),
date =
$.datetimepicker.parseDate(instance.settings.dateFormat ||
$.datetimepicker._defaults.dateFormat, selectedDate, instance.settings);
dates.not(this).datetimepicker("option", option, date);
}
});
Rails:
= text_field_tag :from_js, l((params[:from].to_datetime or
#item.date), format: :american_no_time), class: 'form-control
input-sm datetimepicker', id: 'from'
= text_field_tag :to_js, l((params[:to].to_datetime or
#item.date_end), format: :american_no_time), class: 'form-control
input-sm datetimepicker', id: 'to'
This is used with rails but it seems to be happening in the JS as the only thing that is being called is the class datetimepicker.
Add Startdate and EndDate in date picker
var today = new Date();
var lastDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
In your script add
var today = new Date(); //Get today's date
var lastDate = new Date(today.getFullYear(), today.getMonth() + 1, 0); //To get the last date of today's month
$("#datepicker").datepicker({
startDate: today,
endDate: lastDate
});
This fixed the problem.
var maxDate = new Date();
var minDate = $("#from").val();
$(function () {
$('#from').datetimepicker({
minDate: minDate,
maxDate: maxDate,
pickTime: false
});
$('#to').datetimepicker({
minDate: minDate,
maxDate: maxDate,
pickTime: false
});
});

Setting max date value to current date OR 1 year from start date

What should I do to restrict the user to select the end date beyond one year from the starting date OR the current date. I have my codes like this.
$(function () {
$('#DateFrom').datepicker({
dateFormat:'d-M-y',
changeMonth: true,
changeYear: true,
minDate: new Date(2000, 7, 23),
maxDate: 0,
onSelect: function() {
var date = $('#DateFrom').datepicker('getDate');
date.setTime(date.getTime() + (1000*60*60*24*365));
$('#DateTo').datepicker('option', 'maxDate', date);
$('#DateTo').datepicker('option', 'minDate',$('#DateFrom').datepicker('getDate'));
displayToUser();
},
});
$('#DateTo').datepicker({
dateFormat:'d-M-y',
maxDate: 0,
changeMonth: true,
changeYear: true,
onSelect: displayToUser,
});
});
function displayToUser() {}
Here although it restricts the user within one year from starting date, but it allows the user to select dates beyond the current date.
Example: if start date is 23-01-2016, it allows to select the end date upto 22-01-2017. I want it to be at most today's date.
Does this do it?
$(function () {
$('#DateFrom').datepicker({
dateFormat:'d-M-y',
changeMonth: true,
changeYear: true,
minDate: new Date(2000, 7, 23),
maxDate: 0,
onSelect: function() {
var date = $('#DateFrom').datepicker('getDate');
date.setTime(date.getTime() + (1000*60*60*24*365));
var todaysDate = new Date();
if(date.getTime() > todaysDate.getTime()) date = todaysDate;
$('#DateTo').datepicker('option', 'maxDate', date);
$('#DateTo').datepicker('option', 'minDate',$('#DateFrom').datepicker('getDate'));
displayToUser();
},
});
$('#DateTo').datepicker({
dateFormat:'d-M-y',
maxDate: 0,
changeMonth: true,
changeYear: true,
onSelect: displayToUser,
});
});
function displayToUser() {}
I only added these two lines
var todaysDate = new Date();
if(date.getTime() > todaysDate.getTime()) date = todaysDate;

Disable/Enable selected date range on jQuery datepicker UI

So I have the following demo http://dev.driz.co.uk/week.html that shows a jQuery UI datepicker that has multiple instances for each month of the year.
I've modified it so that the user selects entire weeks and then start and end dates for those weeks are stored on the right hand sidebar with a week number.
What I want to do is disable the dates once the user has selected them so they can see on the calender picker what dates have been selected (and also prevent them from adding the same date range more than once).
However I don't know where to start with this... I've created some enable and disable date functions but don't know how to actually disable the dates using the beforeShowDay method.
For example:
var array = ["2013-03-14","2013-03-15","2013-03-16"]
$('.week-picker').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ array.indexOf(string) == -1 ]
}
});
But how would I disable a range of dates? As I only have the start and end dates. And can I call the beforeShowDay AFTER the datepicker is on the page like in my example? AND how can I then re-enable the dates?
Here's the code:
$(function() {
var startDate;
var endDate;
var selectCurrentWeek = function() {
window.setTimeout(function () {
$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active');
}, 1);
}
$('.week-picker').datepicker( {
defaultDate: '01/01/2014',
minDate: '01/01/2013',
maxDate: '01/01/2015',
changeMonth: false,
changeYear: true,
showWeek: true,
showOtherMonths: true,
selectOtherMonths: true,
numberOfMonths: 12,
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate');
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
addWeek($.datepicker.iso8601Week(new Date(dateText)), $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings ));
disableDates( $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings ));
selectCurrentWeek();
},
beforeShowDay: function(date) {
var cssClass = '';
if(date >= startDate && date <= endDate)
cssClass = 'ui-datepicker-current-day';
return [true, cssClass];
},
onChangeMonthYear: function(year, month, inst) {
selectCurrentWeek();
}
});
$('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
$('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
$('.remove').live('click', function(e){
enableDates($(this).attr('data-startdate'), $(this).attr('data-enddate'));
$(this).parent('div').remove();
});
});
// adds the week to the sidebar
function addWeek(weekNum, startDate, endDate){
$('.weeks-chosen').append('<div data-startdate="'+startDate+'" data-enddate="'+endDate+'"><span class="weekNum">Week '+ (weekNum - 1) +'</span> - <span class="startDate">'+startDate+'</span> - <span class="endDate">'+endDate+'</span> | <span class="remove">X Remove</span></div>');
}
// disable the dates on the calendar
function disableDates(startDate, endDate){
}
// enable the dates on the calendar
function enableDates(startDate, endDate){
}
In short there are two questions here... How do I disable dates AFTER the datepicker is added to the page. And second how do I disable a range between two dates, as it looks like the beforeShowDay method expects an array of dates rather than a range.
But how would I disable a range of dates? As I only have the start and
end dates.
One way could be to create an array of dates based on the start and end dates that you have. Use that array in beforeShowDay to disable the range.
Demo: http://jsfiddle.net/abhitalks/FAt66/1/
For example, Relevant portions of JS:
var startDate = "2014-06-15", // some start date
endDate = "2014-06-21", // some end date
dateRange = []; // array to hold the range
// populate the array
for (var d = new Date(startDate); d <= new Date(endDate); d.setDate(d.getDate() + 1)) {
dateRange.push($.datepicker.formatDate('yy-mm-dd', d));
}
// use this array
beforeShowDay: function (date) {
var dateString = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [dateRange.indexOf(dateString) == -1];
}
Now, you could set startDate and endDate whenever a date is selected. In the example fiddle I linked to above, the start and end dates are set whenever a date is selected in the two top inputs. The data array is populated when date is selected in the second input.
Note: The above example is additive, i.e. everytime you select a new range it gets added as disabled dates into the target. If you want to clear the existing disabled range before specifying a new range, then you could do a destroy and reattach the datepicker. (And also reset the dateRange array)
Demo 2: http://jsfiddle.net/abhitalks/FAt66/3/
Relevant portion of JS:
$("#dt").datepicker("destroy");
$("#dt").datepicker({
dateFormat : 'yy-mm-dd',
beforeShowDay: disableDates
});
var disableDates = function(dt) {
var dateString = jQuery.datepicker.formatDate('yy-mm-dd', dt);
return [dateRange.indexOf(dateString) == -1];
}
Looking at your actual code, all you need is this:
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate');
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
addWeek($.datepicker.iso8601Week(new Date(dateText)), $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings ));
for (var d = new Date(startDate);
d <= new Date(endDate);
d.setDate(d.getDate() + 1)) {
dateRange.push($.datepicker.formatDate('dd/mm/yyyy', d));
}
selectCurrentWeek();
},
beforeShowDay: disableDates,
...
This will keep adding the newly selected date ranges to the array and will additively keep on disabling. But, be cautioned that you will need an escape route when an already selected week is removed. In that case, you may work with multiple array which can be coalesced into one master array.
If there is a requirement to disable a list of dates or like if in any reservation kind of projects where we have to disable some dates throughout the process. So you can use following code,
$(function() {
//This array containes all the disabled array
datesToBeDisabled = ["2019-03-25", "2019-03-28"];
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
minDate : 0,
todayHighlight: 1,
beforeShowDay: function (date) {
var dateStr = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [datesToBeDisabled.indexOf(dateStr) == -1];
},
});
});
I used all the solutions but not worked but i made change in common jquery.datepick.js
Exisiting _isSelectable constructor function
_isSelectable: function(elem, date, onDate, minDate, maxDate) {
var dateInfo = (typeof onDate === 'boolean' ? {selectable: onDate} :
(!$.isFunction(onDate) ? {} : onDate.apply(elem, [date, true])));
//This function is modified by Savata to Block fridays on homepage
return (dateInfo.selectable !== false) &&
(!minDate || date.getTime() >= minDate.getTime()) &&
(!maxDate || date.getTime() <= maxDate.getTime());
}
Changed to
_isSelectable: function(elem, date, onDate, minDate, maxDate) {
var dateInfo = (typeof onDate === 'boolean' ? {selectable: onDate} :
(!$.isFunction(onDate) ? {} : onDate.apply(elem, [date, true])));
return (dateInfo.selectable !== false) &&
(!minDate || date.getTime() >= minDate.getTime()) &&
(!maxDate || date.getTime() <= maxDate.getTime()) && date.getDay() != 5;
/*Added last condition date.getDay() != 5 to block friday
In your case change accordingly
for sunday = 0 to saturday = 6
*/ }

How do i restrict time to one hour in datetimepicker?

I have the following JavaScript code that uses of a StartDate & EndDate field.
What this code does is to restrict the dates on the EndDate field to the day you have chosen in the StartDate field.
What I am trying to do is to restrict time as well, for example the "datetimepicker" consists of a drop-down with Hours.
How do I restrict the EndDate field to one hour(according to the hour I have chosen from the StartDate field plus one) and maybe disable the rest of available hours?
$(document).ready(function () {
var dates = $('#StartDate, #EndDate').datetimepicker({
dateFormat: 'dd/mm/yy',
hourMin: 9,
hourMax: 17,
minDate: '1',
maxDate: null,
controlType: 'select',
timeFormat: 'hh:mm tt',
beforeShowDay: $.datepicker.noWeekends,
firstDay: 1,
changeFirstDay: false,
onSelect: function (selectedDate) {
var option = this.id == "StartDate" ? "minDate" : "maxDate",
instance = $(this).data("datepicker"),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datetimepicker._defaults.dateFormat,
selectedDate, instance.settings);
var edate;
var otherOption;
var d;
if (option == "minDate") {
otherOption = "maxDate";
d = date.getDate() + 0;
}
else if (option == "maxDate") {
otherOption = "minDate";
d = date.getDate() - 0;
}
var m = date.getMonth();
var y = date.getFullYear();
edate = new Date(y, m, d);
dates.not(this).datetimepicker("option", option, date);
dates.not(this).datetimepicker("option", otherOption, edate);
}
});
});
If you are using this plugin:
DateTimePicker
, then you can:
$('#rest_example_3').datetimepicker({
minDate: new Date(2010, 11, 20, 8, 30),
maxDate: new Date(2010, 11, 31, 17, 30)
});
Basically you need pass min date and end date with hours

Categories