I have two text boxes with a datepicker hooked up to them. The text boxes are for start date and end date. The first datepicker is setup so that the user cannot choose a date before today, but can choose any date in the future.
How can I setup the second datepicker so that it cannot choose a date before the date chosen in the first date picker? For example: If today is 12/11/10 and I choose 12/15/10 in the first datepicker, then the second date picker shouldn't be able to choose anything before 12/15/10.
Heres what I have so far:
$("#txtStartDate").datepicker({ minDate: 0 });
$("#txtEndDate").datepicker({});
For example, in this sample code, startDatePicker is selected as 2010-12-12, change event of startDatePicker sets the minDate of endDatePicker 2010-12-13. It locks the cells before this date. This is a sample for what #Victor mentioned..I hope it helps...Regards...Ozlem.
$("#startDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
minDate: new Date(),
maxDate: '+2y',
onSelect: function(date){
var selectedDate = new Date(date);
var msecsInADay = 86400000;
var endDate = new Date(selectedDate.getTime() + msecsInADay);
//Set Minimum Date of EndDatePicker After Selected Date of StartDatePicker
$("#endDatePicker").datepicker( "option", "minDate", endDate );
$("#endDatePicker").datepicker( "option", "maxDate", '+2y' );
}
});
$("#endDatePicker").datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true
});
Update:
The approach above set the minDate only on creation time.
I used the onSelect event to change the minDate option of the second datepicker like this:
$("#txtStartDate").datepicker({
showOn: "both",
onSelect: function(dateText, inst){
$("#txtEndDate").datepicker("option","minDate",
$("#txtStartDate").datepicker("getDate"));
}
});
the Tin Man's solution worked for me after adding $("#txtEndDate").datepicker() at the bottom
$("#txtStartDate").datepicker({
showOn: "both",
onSelect: function(dateText, inst){
$("#txtEndDate").datepicker("option","minDate",
$("#txtStartDate").datepicker("getDate"));
}
});
$("#txtEndDate").datepicker(); //it is not working with out this line
try this man:
$("#dateTo").datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
minDate: new Date()
}).datepicker("setDate", new Date());
$("#dateFrom").datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
changeYear: true,
onSelect: function(){
$('#dateTo').datepicker('option', 'minDate', $("#dateFrom").datepicker("getDate"));
}
}).datepicker("setDate", new Date());
$('#start_date').datepicker({
endDate: new Date(),
autoclose: true,
}).on("changeDate", function (e) {
$('#end_date').datepicker('setStartDate', e.date);
});
$('#end_date').datepicker({
autoclose: true,
});
From a pure UI standpoint, you shouldn't. If the user picks the wrong month on both datepickers, and tries to select the correct month, he has a 50% change of being hindered by the UI. Ideally, you would allow the incorrect selection and display a helpful error message saying the end date should be after the end date.
If you wish to implement your idea : give each datepicker a "change" event that changes the options on the other datepicker appropriately.
Use the "getDate" method of the first datepicker UI and pass it into minDate option of the second datepicker:
$("#txtEndDate").datepicker({
showOn: "both",
minDate: $("#txtStartDate").datepicker("getDate")
});
Use This Code:
<script type="text/javascript">
$(function () {
$("#txtStartDate").datepicker({
dateFormat: 'dd/mm/yy',
inline: true,
minDate: 'dateToday'
});
$("#txtEndDate").datepicker({
dateFormat: 'dd/mm/yy',
inline: true,
minDate: $("#txtStartDate").datepicker("getDate") });
});
</script>
Hi everyone going to show what works with me in this case:
2 Datepickers ( DateFrom and DateTo)
HTML:
<!-- Datapicker dateFrom -->
<label for="dateFrom"> Date from: </label>
<div>
<input type="text" id="dateFrom" class="form-control" autocomplete="off"
th:placeholder="Enter a date From.."/>
</div>
<!-- Datapicker dateTo-->
<label for="dateTo"> Date to: </label>
<div>
<input type="text" id="dateTo" class="form-control" autocomplete="off"
th:placeholder="Enter a date to..."/>
</div>
Next you build your datapickers in JavaScript:
Datapicker activation (With YOUR datapicker build requirements)
$("#dateFrom").datepicker({
dateFormat: 'yy-mm-dd',
showButtonPanel: true
});
$('#dateTo').datepicker({
dateFormat: 'yy-mm-dd',
showButtonPanel: true
});
-And finally on the OnChange Event, for every time a Date is picked in any of the datepickers the selectable range avaliable in the other Datapicker changes.
$("body").on("change", "#dateFrom", function() {
$('#dateTo').datepicker('option', 'minDate', $("#dateFrom").datepicker("getDate"));
});
$("body").on("change", "#dateTo", function() {
$('#dateFrom').datepicker('option', 'maxDate', $("#dateTo").datepicker("getDate"));
});
This make the DateTo DataPicker limit on change the date of the DateFrom Datapicker and viceversa.
$startDateCtrl.change(function () {
setMinEndDateValue($startDateCtrl, $endDateCtrl);
});
$endDateCtrl.datepicker();
I have two Date picker start and end.
End Date min and max date we are setting based on the selection from start.
Min start date for End date picker is start date from start picker selection.
Max end date for end date picker is selected start date from start date picker + 7 days.
function setMinEndDateValue($startDateCtrl, $endDateCtrl) {
var $maxSchedulingDate = new Date(#MaxDate.Year, #MaxDate.Month -1, #MaxDate.Day );
var $startDate = $startDateCtrl.val();
var $selectedStartDate = new Date($startDate);
$selectedStartDate.setDate($selectedStartDate.getDate() + 7); // increasing the start date by 7 days (End Date max)
var $maxEndDate = new Date();
if ($selectedStartDate > $maxSchedulingDate) {
$maxEndDate = $maxSchedulingDate;
}
else {
$maxEndDate = $selectedStartDate;
}
$endDateCtrl.datepicker("option", "minDate", $startDate);
$endDateCtrl.datepicker("option", "maxDate", $maxEndDate);
}
Building on the previous answers in this thread, I felt a solution that worked with defaults and reapplied both min and max dates would be useful:
// Defaults
var defaultFromDate = new Date();
var defaultToDate = new Date();
defaultToDate.setMonth(defaultToDate.getMonth() + 1);
// To
$("#datepickerTo").datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true,
});
$("#datepickerTo").datepicker("setDate", defaultToDate);
function limitToDateSpan(currentFromDate) {
$("#datepickerTo").datepicker("option", "minDate", currentFromDate);
var maxDate = new Date(currentFromDate.getTime());
maxDate.setFullYear(maxDate.getFullYear() + 1);
$("#datepickerTo").datepicker("option", "maxDate", maxDate);
}
limitToDateSpan(defaultFromDate);
// From
$("#datepickerFrom").datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true,
minDate: "2013-01-01",
maxDate: "+1Y",
onSelect: function (dateText) {
limitToDateSpan(new Date(dateText));
}
});
$("#datepickerFrom").datepicker("setDate", defaultFromDate);
$("#<%=txtFromDate.ClientID%>").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-M-yy',
showOn: "button",
buttonImage: "../Images/Calendar.png",
buttonImageOnly: true,
yearRange: '-20:+20',
buttonText: "Date with abbreviated month name",
onSelect: function (selected) {
var dt = new Date(selected);
dt.setDate(dt.getDate() + 1);
$("#<%=txtToDate.ClientID%>").datepicker("option", "minDate", dt);
}
});
$("#<%=txtToDate.ClientID%>").datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'dd-M-yy',
showOn: "button",
buttonImage: "../Images/Calendar.png",
buttonImageOnly: true,
yearRange: '-20:+20',
buttonText: "Date with abbreviated month name",
onSelect: function (selected) {
var dt = new Date(selected);
dt.setDate(dt.getDate() - 1);
$("#<%=txtFromDate.ClientID%>").datepicker("option", "maxDate", dt);
}
});
I want to set mindate on basis of first datetimepicker, its working fine first time but when I setting for second time , its not working properly. What I missing, any suggestion.
Thanks in advance.
$('#start_dateTime').datetimepicker({
minDate: moment()
}).on('dp.change', function (event) {
var start_dateTime = this.value;
$('#end_dateTime').attr('readonly', false);
$('#end_dateTime').datetimepicker({
minDate: moment(start_dateTime)
})
});
Below is the code for jQuery without using Moment This is for DatePicker Same is applicable for Datetimepicker too.
jQuery(document).ready(function ($) {
$('#startdate_0').datepicker({
//changeMonth: true, changeYear: true,
dateFormat: "yy-mm-dd",
minDate: new Date(),
onClose: function (selectedDate) {
$("#enddate_0").datepicker("option", "minDate", selectedDate);
}
});
$('#enddate_0').datepicker({
// changeMonth: true, changeYear: true,
dateFormat: "yy-mm-dd",
onClose: function (selectedDate) {
$("#startdate_0").datepicker("option", "maxDate", selectedDate);
}
});
});
i have looked around before posting my question
following what i am looking in my datepicker (start date and end date):
1) Start date: can be any date, (user can select start date current (now) to any future date.
2) Start date: user can select start date as back as 6 months.
example: if today is 04/22/2010 then i can go back up to 11/22/2009 but not more than 6 moths.
3) Start date if the user select the start date (current of future, not past) less then 10 days then i would like to alert message saying "need at least 10 days"
4) End date: should be current date to future dates alll previous dates are disabled.
5) **Start date / End date: ** should not be greater than one year.
Thanks so much.
PS: not sure if i am asking too much here :)
I answered a similar question for a friend. He needed a cascading date similar, but I think you can see how to solve the issue if you look at my markup.
HTML
<form target="_self" action="ClearForm.aspx">
Clear Form
Start Date:
End Date:
Clear
JS
<script type="text/javascript">
$(document).ready(function () {
$('#endDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect: function () { },
onClose: function () { $(this).focus(); }
});
$('#startDate').datepicker({ showOn: 'button',
buttonImage: '../images/Calendar.png',
buttonImageOnly: true, onSelect:
function (dateText, inst) {
$('#endDate').datepicker("option", 'minDate', new Date(dateText));
}
,
onClose: function () { $(this).focus(); }
});
});
<input type="text" id="tbStartDate" value="" disabled="disabled" />
<input type="text" id="tbEndDate" value="" disabled="disabled" />
<script type="text/javascript">
$(document).ready(function () {
$("#tbStartDate").datepicker({
showOn: 'button',
buttonImageOnly: true,
buttonImage: '/Content/Calendar.png',
buttonText: 'Click here (date)',
onSelect: function (dateText, inst) {
var $endDate = $('#tbStartDate').datepicker('getDate');
$endDate.setDate($endDate.getDate() + 1);
$('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
},
onClose: function (dateText, inst) {
//$("#StartDate").val($("#tbStartDate").val());
}
});
$("#tbEndDate").datepicker({
dateFormat: 'dd-mm-yy',
showOn: 'button',
buttonImageOnly: true,
buttonImage: '/Content/Calendar.png',
buttonText: 'Click here (date)',
onClose: function (dateText, inst) {
//$("#EndDate").val($("#tbEndDate").val());
}
});
var $endDate = $('#tbStartDate').datepicker('getDate');
$endDate.setDate($endDate.getDate() + 1);
$('#tbEndDate').datepicker('setDate', $endDate).datepicker("option", 'minDate', $endDate);
});
</script>
I prefer to write a default configuration for the datepicker using a class so that it works with more than one with less duplicates.
onSelect: function(dateText, inst){
// Check if _until exists and auto set mindate
if(inst.id.contains("_from")){
$("#"+inst.id.replace("_from", "_until")).datepicker("option", "minDate", dateText);
}
}
This works assuming your form IDs are consistent. E.g I use date_until and date_from
I get this working it gets on week selection "Week number: #" on main field and on alternate field get "yy-mm-dd"
On load I get "yy-mm-dd" from current date (Today) On both fields, but I want on load "Week number: #" on main and "yy-mm-dd" on alternate, from current date.
$(function() {
var currentDate = new Date();
$( "#fecha" ).datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showWeek: true,
firstDay: 1,
dateFormat: 'yy-mm-dd',
altField: "#date1",
altFormat: "yy-mm-dd",
onSelect: function(dateText, inst) {
$(this).val("Semana Numero: " + $.datepicker.iso8601Week(new Date(dateText)));
}
});
$("#fecha").datepicker("setDate", currentDate);
});
Any suggestion?
I'm not very familiar with the datepicker plugin, but it looks like it doesn't have any events you can trigger on it.
Anyways, this might not be the best approach, but what I did was loop through all the datepicker date <a>'s and trigger a click on the date that matches today's date.
https://jsfiddle.net/b3vstmef/
I was just looking over the datepicker and it doesnt look like you can trigger the onselect so I found that the code below will work instead.
$(function() {
var currentDate = new Date();
$( "#fecha" ).datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showWeek: true,
firstDay: 1,
dateFormat: 'yy-mm-dd',
altField: "#date1",
altFormat: "yy-mm-dd",
onSelect: function(dateText, inst) {
populate(this, dateText)
}
});
$("#fecha").datepicker("setDate", currentDate);
});
function populate(field, d){
$(field).val("Semana Numero: " + $.datepicker.iso8601Week(new Date(d)));
}
populate($( "#fecha" ), $("#date1").val());
See it in action:
https://jsfiddle.net/y3llowjack3t/ycok5cwx/
I use the datepicker from jQuery UI, but the format is like example today: 08/01/2013
But i want the format to be example today: 2013-08-01
Here is code line i think maybe i should use?
`$( "#txtFromDate" ).datepicker( "option", "dateFormat", "yyyy-mm-dd" );`
And here is my jQuery code i use that works fine but don't know where to put the line?
And i want the format in both of my fields.
`
$(document).ready(function(){
$("#txtFromDate").datepicker({
minDate: "07/17/2012",
maxDate: "0D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected);
}
});
$("#txtToDate").datepicker({
minDate: "07/17/2013",
maxDate:"0D",
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected);
}
});
});`
I hope somebody have a solution for this :-)
since your already have other options in the datepicker... add it there.
try this
$("#txtFromDate").datepicker({
minDate: "07/17/2012",
maxDate: "0D",
numberOfMonths: 2,
dateFormat: "yy-mm-dd", //<----here
onSelect: function(selected) {
$("#txtToDate").datepicker("option","minDate", selected);
}
});
You can directly set the date format from jquery-ui library,that's work for me.hope for you also
in
function Datepicker()
{
//other code
dateFormat: "yy-mm-dd", // See format options on parseDate
//other code
}