Set today as a start day of the month in date picker - javascript

Can we set the start date of the month to today and the end to that date in next month.
For example: today is 01/28/2013
Calendar should display from 01/28/2013 to 02/27/2013
Thanks in advance.

try this (-1D to get 27 of next month otherwise it would be same of today date):
$(function() {
$( "#datepicker" ).datepicker({ minDate: 0, maxDate: "+1M -1D" });
});

Yes you can:
Do this:
$(document).ready(function(){
$("#txtDate").datepicker({ minDate: 0, maxDate: '+1M', numberOfMonths:1 });
});

Related

Bootstrap 3 datetimepicker - MinTime and MaxTime

i am trying to set a min time and max time for https://getdatepicker.com/4/
$mydatepicker.datetimepicker({
minDate: moment("12/01/2015 8:50 AM"),
maxDate: moment("12/30/2015 9:50 AM"),
viewMode: 'days',
format: 'DD MMMM YYYY hh:mm A',
});
It is not restricting to the given time if we change AM to PM its changing and the date is removed if we try to increment the minutes. Any one have idea of how to use this plugin for setting minimum date and also time?
I think that moment.js is deprecated. You could set like this with jQuery:
$('#datepicker1').datepicker({
dateFormat: "yy-mm-dd",
maxDate: new Date('2018-3-26')
});
// Number
$('#datepicker2').datepicker({
dateFormat: "yy-mm-dd",
maxDate: 2
});
// String
$('#datepicker3').datepicker({
dateFormat: "yy-mm-dd",
maxDate: "+1m"
});
Same implies to minDate.

SetDate on jquery Datepicker onClose does not change value

i have a table and each row has a datepicker. Datepickers are only selecting months and on close i set the first day of the month except if this day is before the minDate so i want to set minDate there.
SetDate function does not work. My log gives me that properDate is set correctly but when i pass it in the setDate the result does not change and somehow it is stack to first Day of the month. What am i missing?
function initDatepickerSchedule(){
$(".datepickerSchedule").each(function(){
$(this).datepicker({
showOn: "both",
changeMonth: true,
changeYear: true,
dateFormat: "yy-mm-dd",
minDate: scheduleTaskMinDate(),
maxDate: scheduleTaskMaxDate($(this).closest("tr").find(".ScheduledTaskQtyInput").val(),$(this).closest("tr").find(".ScheduledTaskCompletedQtyInput").val()),
showButtonPanel: true,
onClose: function(dateText, inst) {
function isDonePressed(){
return $('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1;
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
var properDate=new Date(year, month, 1);
var minDate=$(this).datepicker("option","minDate");
console.log(properDate);// 2019-12-1
console.log(minDate);//2019-12-2
console.log(properDate<minDate);// true
if(properDate<minDate){
properDate=minDate;
}
console.log("before set");
console.log(properDate===minDate);//true
console.log(properDate);//2019-12-2
$(this).datepicker("setDate",properDate);
console.log($(this).datepicker("getDate"));//2019-12-1
var coNo=$(this).closest("tr").find(".changeOrderNoInput").val();
$(this).closest("tr").find(".ScheduledTaskVersion").val(studyTaskScheduleVersion($(this).datepicker("getDate"),coNo));
$("#SubmitScheduleDistButton").prop('disabled', true).prop('title',"Validate without Errors to Unlock");
}
}
}).focus(function(){
$(".ui-datepicker-calendar").hide();
$(this).attr("autocomplete","off");
});
});
}
It seems that max Date was really relevant. In this specific case i should have a date inside the same month for the maxDate, but for some reason (i do not remember and checked possible influence in other parts of the program but i can not find any problem)i was returning the first date of the month.
So this was not shown on the datepicker case calendar was hidden so i could not know.
And this is way it was overriding the set function cause of the maxDate.

Restricting second date in jQuery UI Datepicker [duplicate]

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);
}
});

Date picker using jquery for dob year validation

I am using date picker from Jquery. Where currently I facing one problem By default the calendar will show till 2100 years but if the user select any feature date or current date the system should not accept. The system should accept the date minimum 10 year previous from todays date.
I am bit confused how to do I tried with min year & max year but If i gave year range as 1900 to 2015 calendar was showing till 2015 sep only but i want to show till 2100
Here is the jsbin Link
Guys kindly help me
Here is the Jquery code
$("#txt_dob").datepicker({
dateFormat: "mm-dd-yy",
changeMonth: true,
changeYear: true,
//showButtonPanel: true,
yearRange: '-115:+10',
beforeShow: function () {
setTimeout(function (){
$('.ui-datepicker').css('z-index', 99999999999999);
}, 0);
},
}).on('change', function() {
if($('#txt_dob').valid()){
$('#txt_dob').removeClass('errRed');
}
// triggers the validation test on change
});
Thanks in advance
My solution would be to create a Date object:
var date = new Date(); // current date
Then substract 10 years:
date.setFullYear(date.getFullYear() - 10)
Then affect this date to be the maximum date of your date picker
$("#txt_dob").datepicker({
dateFormat: "mm-dd-yy",
maxDate: date,
changeMonth: true,
changeYear: true
});
Use the maxDate property of the date picker as follows:
maxDate: '-10Y'

jQuery: datepicker, alert the dayname of the selected

How can i alert the selected days name? Example 'Monday'.
So when you pick 7th june 2011 it will alert "Tuesday"
<script>
$(function() {
$( "#date" ).datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function(dateText, inst) {
// how can i grab the day name of the day, example "Monday" and alert it out?
// alert( ? );
}
});
});
</script>
The jQueryUI's Datepicker comes with a formatDate function that can do that for you. If you're using a localized version, it'll show the days in that language too.
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate');
alert($.datepicker.formatDate('DD', date));
}
For more information on localization of on Dapicker's utility functions, have a look at http://jqueryui.com/demos/datepicker/.
<script>
$(function() {
$( "#date" ).datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function(dateText, inst) {
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
alert(weekday[inst.getDate().getDay()];
}
});
});
</script>
this will work if you don't mind showing the name of the day in the input box
if you dont like it you can use second hidden input (http://jqueryui.com/demos/datepicker/#alt-field)
$(function() {
$( "#date" ).datepicker({
dateFormat: 'DD, d MM, yy',
onSelect: function(dateText, inst) {
var stop = dateText.indexOf(',');
alert( dateText.substring(0, stop));
}
});
});
example at http://jsfiddle.net/aa74R/
You can parse a date like this (mm/dd/yyyy):
new Date(Date.parse("06/07/2011"))
and use the .getDay function. You could do this:
// parse data
var regexp = /(\d{2})\/(\d{2})\/(\d{2})/.exec("07/06/11");
//form date
var date = new Date(Date.parse(regexp[2] + "/" + regexp[1] + "/20" + regexp[3]));
alert(date.getDay()); // 2 -> Tuesday (starts at 0 = Sunday)
Use inbuilt function
$("#datepicker" ).datepicker({ onSelect: function(dateText, inst) {
alert($.datepicker._defaults.dayNames[new Date(dateText).getDay()]);
}});

Categories