Is DateTimePicker cannot disable past time? - javascript

I want to disable past time and date based on my selection of start date and end date. I was able to disable my date, but it won't work on time.
Example:
Start Date - 24-May-2019 01:30 PM
End Date - 24-May-2019 01:00 PM < suppose to not be able to choose an earlier time
i tried this function, but while disable past date is working fine, still cannot for time
onSelect: function (selectedDate) {
var endYear = $(this).datetimepicker('getFullYear');
var endMonth = $(this).datetimepicker('getMonth');
var endDay = $(this).datetimepicker('getDate');
var endHour = $(this).datetimepicker('getHours');
var endMinutes = $(this).datetimepicker('getMinutes');
var endSeconds = $(this).datetimepicker('getSeconds');
("#endDate").datetimepicker("option", "minDate", new Date(endYear, endMonth, endDay, endHour, endMinutes, endSeconds));
}
onSelect: function (selectedDate) {
var endYear = $(this).datetimepicker('getFullYear');
var endMonth = $(this).datetimepicker('getMonth');
var endDay = $(this).datetimepicker('getDate');
var endHour = $(this).datetimepicker('getHours');
var endMinutes = $(this).datetimepicker('getMinutes');
var endSeconds = $(this).datetimepicker('getSeconds');
$("#startDate").datetimepicker("option", "maxDate", new Date(endYear, endMonth, endDay, endHour, endMinutes, endSeconds));
}
also tried this function below, but also not working
onSelect: function (selectedDate) {
$("#endDate").datetimepicker("option", "minDate", selectedDate);
$("#endDate").datetimepicker("option", "minTime", selectedDate);
}
onSelect: function (selectedDate) {
$("#startDate").datetimepicker("option", "maxDate", selectedDate);
$("#startDate").datetimepicker("option", "maxTime", selectedDate);
}
current working code (that disable past date only):
for start date
onSelect: function (selectedDate) {
$("#endDate").datetimepicker("option", "minDate", selectedDate);
}
WORKING CODE
added an onClose function to read the time
for example :
if user select start date 05-May-2019 5:00 AM and end date 05-May-2019 3:00 AM
the start date value will automatically update to 05-May-2019 3:00 AM
for end date
onSelect: function (selectedDate) {
$("#startDate").datetimepicker("option", "maxDate", selectedDate);
}
for startDate
onClose: function (dateText, inst) {
var startDateSelect = $('#startDate');
var endDateSelect = $('#endDate');
if (endDateSelect.val() != '') {
var sDate = startDateSelect.datetimepicker('getDate');
var eDate = endDateSelect.datetimepicker('getDate');
if (sDate > eDate)
endDateSelect.datetimepicker('setDate', sDate);
}
else {
endDateSelect.val(dateText);
}
},
onSelect: function (selectedDate) {
$("#endDate").datetimepicker("option", "minDate", selectedDate);
}
for endDate
onClose: function (dateText, inst) {
var startDateSelect = $('#startDate');
var endDateSelect = $('#endDate');
if (startDateSelect.val() != '') {
var sDate = startDateSelect.datetimepicker('getDate');
var eDate = endDateSelect.datetimepicker('getDate');
if (sDate > eDate)
startDateSelect.datetimepicker('setDate', eDate);
}
else {
startDateSelect.val(dateText);
}
},
onSelect: function (selectedDate) {
$("#startDate").datetimepicker("option", "maxDate", selectedDate);
}

For using jquery DateTimePicker, there are option for setting the min/max time as well.
use minTime or maxTime for times
use minDate or maxDate for dates
Working example: https://jsfiddle.net/qy48so6d/1/
$('#datetimepicker').datetimepicker({
minDate:'2019/05/01',
maxDate:'2019/05/02',
minTime:'01:00',
maxTime:'02:00'
});
Reference: https://xdsoft.net/jqplugins/datetimepicker/#minTime
If you are using https://trentrichardson.com/examples/timepicker/
you can try this
$("#endDate").datetimepicker("option", "minDateTime", selectedDate);
// or
$("#endDate").datetimepicker("option", "minTime", '8:00 am');
Example
$(document).ready(function() {
var startDateTextbox = $('#startDate')
var endDateTextbox = $('#endDate')
startDateTextbox.datetimepicker({
onSelect: function(selectedDate) {
endDateTextbox.datetimepicker('option', 'minDate', startDateTextbox.datetimepicker('getDate'))
endDateTextbox.datetimepicker('option', 'minTime', '8:00 am')
endDateTextbox.datetimepicker('option', 'maxTime', '9:00 am')
}
});
endDateTextbox.datetimepicker({
minTime: '8:00 am'
});
})

Related

I have problem with datepicker dateDisnable

I hava a problem with use datepicker bootstrap v1.5.0
var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
var checkin = $('.checkin').datepicker({
todayHighlight:'TRUE',
autoclose: true,
beforeShowDay: function(date) {
return date.valueOf() >= now.valueOf();
},
datesDisabled :test,
}).on('changeDate', function(ev) {
$(this).datepicker('hide');
if (ev.date.valueOf() > checkout.datepicker("getDate").valueOf()
|| !checkout.datepicker("getDate").valueOf()) {
var newDate = new Date(ev.date);
newDate.setDate(newDate.getDate() + 1);
checkout.datepicker("update", newDate);
}
});
var checkout = $('.checkout').datepicker({
beforeShowDay: function(date) {
if (!checkin.datepicker("getDate").valueOf()) {
return date.valueOf() >= new Date().valueOf();
} else {
return date.valueOf() > checkin.datepicker("getDate").valueOf();
}
},
autoclose: true,
datesDisabled :daylist,
}).on('changeDate', function(ev) {});
when I choose date of my checkin input, my checkout input not display, but when i press tab and click again, it display and with error
enter image description here
It appears you may have syntax errors:
remove comma ","
datesDisabled :test,
datesDisabled :test
and also here:
datesDisabled :daylist,
datesDisabled :daylist

How to re-initialize the bootstrap datetimepicker?

I have a Two datetimepicker on my page. Name as 'SartDate' and 'EndDate'.Select the date on StartDate, this date set as a minDate of 'EndDate'.First time is work Correctly.But the date of 'StartDate' reset, the minDate of 'EndDate' Not
re-initiliaz
var date = null;
$("#jobPublishDate").bind("keyup blur change mouseup", function () {
$('#jobPublishDate').datetimepicker({ format: 'DD/MM/YYYY H:mm', showTodayButton: true, showClear: true, showClose: true });
var from = $("#jobPublishDate").val().split("/");
var newdate = from[1] + "/" + (from[0]) + "/" +from[2];
var f = new Date(newdate);
date = f;
});
$("#jobCloseDate").focusin(function () {
$('#jobCloseDate').datetimepicker({ format: 'DD/MM/YYYY H:mm', showTodayButton: true, showClear: true, showClose: true, minDate: date });
});
});
<label>StartDate></label>
<input ng-model="job.StrPublishDate" id="jobPublishDate">
<br/>
<label>EndDate></label>
<input ng-model="job.StrCloseDate" id="jobCloseDate">
You need to hook to the dp.change event (fired when the date has been selected) an use minDate method to set the minimum date on the second picker.
var date = null;
$("#jobPublishDate").bind("keyup blur change mouseup", function () {
$('#jobPublishDate').datetimepicker({ format: 'DD/MM/YYYY H:mm', showTodayButton: true, showClear: true, showClose: true })
var from = $("#jobPublishDate").val().split("/");
var newdate = from[1] + "/" + (from[0]) + "/" +from[2];
var f = new Date(newdate);
date = f;
});
// Adds the date change event to the first picker
$("#jobPublishDate").on('db.change', function (e) {
// Get the DateTimePicker instance
var dp = $('#jobCloseDate').data('DateTimePicker');
// If the second picker has been initialized
if(typeof dp !== 'undefined') {
// Set the minimum date on the second picker
dp.minDate(e.date);
} else {
// If the close date picker is not initialized, stores the date for later use
date = e.date;
}
});
$("#jobCloseDate").focusin(function () {
$('#jobCloseDate').datetimepicker({ format: 'DD/MM/YYYY H:mm', showTodayButton: true, showClear: true, showClose: true, minDate: date });
});
You can find out more about DateTimePicker methods here.
var date = null;
$("#jobPublishDate").bind("keyup blur change mouseup", function () {
var from = $("#jobPublishDate").val().split("/");
var newdate = from[1] + "/" + (from[0]) + "/" + from[2];
var f = new Date(newdate);
date = f;
$("#jobPublishDate").on('dp.change', function (e) {
var dp = $('#jobCloseDate').data('DateTimePicker');
if (typeof dp !== 'undefined') {
dp.minDate(e.date);
}
else {
date = e.date;
}
});
$("#jobCloseDate").focusin(function () {
$('#jobCloseDate').datetimepicker({ format: 'DD/MM/YYYY H:mm', showTodayButton: true, showClear: true, showClose: true, minDate: date });
});
Fellow this Code and Solve the Problem.

Jquery ui datepicker disappears on android mobile

I have a big trouble with android mobile on my web site when I click my calendar datepicker appears and after keyboard hiding my datepicker hides, I couldn't find the problem for months.
On IOS and desktop version I have no problem and I have this trouble only with android devices.
I don't think problem is about with my calendar function but I want to share with you:
var dateFormat = "DD/MM/YY",
from = $("#checkin,.checkin").datepicker({
firstDay: 1,
minDate: 0,
showButtonPanel: true,
closeText: 'Temizle',
onClose: function(dateText, inst) {
},
onSelect: function(selectedDate) {
window.setTimeout($.proxy(function() {
$(this).parents(".book-holiday").find("#checkout,.checkout").focus();
}, this), 10);
var yenitarih = new Date();
var date = $(this).datepicker('getDate');
var checkoutStartDate = new Date(new Date(date).setDate(date.getDate() + 1));
var checkoutEndDate = new Date(new Date(date).setDate(date.getDate() + 500000));
$("#checkout,.checkout").datepicker("option", "minDate", checkoutStartDate);
$("#checkout,.checkout").datepicker("option", "maxDate", checkoutEndDate);
},
isTo1: true,
beforeShowDay: function(date) {
var selectedDate = $(this).datepicker('getDate'),
fromDate = from.datepicker('getDate');
if (!fromDate || !selectedDate) {
return [true]
}
var dateClass = '';
var dateClass = '';
if (date > fromDate && date < selectedDate) {
dateClass = 'start';
} else if (+date == +selectedDate) {
dateClass = 'stop';
}
return [true, dateClass];
},
beforeShow: function(input, inst) {
$(this).datepicker("widget").addClass("main-datepicker");
controlDatepicker(".checkin,#checkin");
}
});
$("#checkout,.checkout").datepicker({
firstDay: 1,
minDate: 0,
showButtonPanel: true,
closeText: 'Temizle',
onClose: function(dateText, inst) {
},
isTo1: true,
onSelect: function(selectedDate) {
$(this).parents(".book-holiday").find(".popover-wrapper").addClass("open");
$(this).parents(".book-holiday").find(".popover-wrapper").addClass("open");
},
beforeShowDay: function(date) {
var selectedDate = $(this).datepicker('getDate'),
fromDate = from.datepicker('getDate');
if (!fromDate || !selectedDate) {
return [true]
}
var dateClass = '';
if (date > fromDate && date < selectedDate) {
dateClass = 'start';
} else if (+date == +selectedDate) {
dateClass = 'stop';
}
return [true, dateClass];
},
beforeShow: function(input, inst) {
$(this).datepicker("widget").addClass("main-datepicker");
controlDatepicker(".checkin,#checkin");
}
I set readonly to my datepicker input but when I click it keyboard is showing I don't understand why is it opening ?
and I want to share my web site: click to see - please check it out on android devices and click to datepicker

datetimepicker update mindate maxdate

I am using jquery datetimepicker for two textboxes From date To date. I want to update the values mindate and maxdate from the other text box. The first time it is working fine but if I change the values on debug are correct but the datetimepicker doesnt update. My question is how I will make datetimepicker to update. Reading the code I think it is more clear. Thank you
$(".DatePickerFieldOnHire").live('click', function () {
var defaultDateTime = "";
var dateFrom = "";
var dateTo = "";
dateFrom = document.getElementById('WebReportForm1_Value_DeliveryDate').value;
dateTo = document.getElementById('WebReportForm1_Value_ExpectedRedeliveryDate').value;
var now = new Date();
var dateNow=now.format("dd-mm-yy HH:mm z");
dateFrom = document.getElementById('WebReportForm1_Value_DeliveryDate').value;
dateTo = document.getElementById('WebReportForm1_Value_ExpectedRedeliveryDate').value;
var now = new Date();
var dateNow=now.format("dd-mm-yy HH:mm z");
if (dateFrom == null) {
dateFrom = new Date(1999, 10 - 1, 25);
}
if (dateTo == null) {
dateTo = new Date(2055, 10 - 1, 25);
defaultDateTime = dateNow;
} else {
if (dateTo > dateNow) {
defaultDateTime = dateNow;
}
else {
defaultDateTime = dateTo;
}
}
var inputOff = "";
inputOff = $('[id*="OffHireDateField"]').val();
if (inputOff != "") {
dateFrom = inputOff;
}
$(this).datetimepicker({
showOn: 'focus', showTimezone: false, ampm: true, dateFormat: "dd-mm-yy",
timeFormat: "HH:mm z",
minDate: dateFrom,
maxDate: dateTo,
showTimezone: true,
defaultDate: defaultDateTime,
onSelect: function () { }
}).focus();
});
and the From textbox
$(".DatePickerFieldOffHire").live('click', function () {
var dateFrom = "";
var dateTo = "";
dateFrom = document.getElementById('WebReportForm1_Value_DeliveryDate').value;
dateTo = document.getElementById('WebReportForm1_Value_ExpectedRedeliveryDate').value;
if (dateFrom == null) {
dateFrom = new Date(1999, 10 - 1, 25);
}
if (dateTo == null) {
dateTo = new Date(2055, 10 - 1, 25);
defaultDateTime = dateNow;
} else {
if (dateTo > dateNow) {
defaultDateTime = dateNow;
}
else {
defaultDateTime = dateTo;
}
}
var inputOn = "";
inputOn = $('[id*="OnHireDateField"]').val();
if (inputOn != "") {
dateTo = inputOn;
}
$(this).datetimepicker({
showOn: 'focus', showTimezone: false, ampm: true, dateFormat: "dd-mm-yy",
timeFormat: "HH:mm z",
minDate: dateFrom,
maxDate: dateTo,
showTimezone: true,
defaultDate: defaultDateTime,
onSelect: function () { }
}).focus();
});
I have to admit, I did not check the code thoroughly as you have not included a JSFiddle, but from what I can see, it looks like the first time, dateFrom and dateTo are Date objects but from the second time on they are Strings.
This might be an issue.
Your date format dd-mm-yy does not comply with the default format yy-mm-dd.

Disable days in Datepicker

I'm trying to disable certain days of the calendar, but I can not.
I'm using this datepicker https://github.com/eternicode/bootstrap-datepicker/blob/master/docs/index.rst.
The javascript code I have is this, I work all I need except the days you want to disable.
var disabledDays = ['11/29/2013', '11/27/2013', '11/28/2013'];
function daysDisabled(date) {
for (var i = 0; i < disabledDays.length; i++) {
if (new Date(disabledDays[i]).toString() == date.toString()) {
return [false, ''];
}
}
return [true, ''];
}
$('.datepicker').datepicker({
format: 'dd-mm-yyyy',
todayHighlight: true,
autoclose: true,
weekStart: 1,
startDate: '0d',
language: 'es',
beforeShowDay: daysDisabled
})
just modify your function like this:
function daysDisabled(date) {
for (var i = 0; i < disabledDays.length; i++) {
if (new Date(disabledDays[i]).toString() == date.toString()) {
return false;
}
}
return true;
}
Created Fiddle for you.
Working Fiddle
function initComponent(){
/* Date retrait */
$("#dateRetrait").datepicker({
dateFormat: 'dd-mm-yy',
minDate: new Date(),
beforeShowDay: function(d) {
var dmy = (d.getMonth()+1);
if(d.getMonth()<9)
dmy="0"+dmy;
dmy+= "-";
if(d.getDate()<10) dmy+="0";
dmy+=d.getDate() + "-" + d.getFullYear();
console.log(dmy+' : '+($.inArray(dmy, disbleddates)));
if ($.inArray(dmy, disbleddates) != -1) {
return [false, "","Available"];
} else{
return [true,"","unAvailable"];
}
}
});

Categories

Resources