So i seem to have found a problem im not sure if this is jQuery bug or it's just not possible but if any one has managed this could they tell me how.
I have a Date picker and i don't want to allow users to select dates in the past.
var d = new Date($(this).val());
var nowDate = new Date();
var nowDateParse = ""+
nowDate.getFullYear()+
(nowDate.getMonth()+1 < 10? "0"+(nowDate.getMonth()+1): (nowDate.getMonth()+1))+
(nowDate.getDate()+1 < 10? "0"+(nowDate.getDate()+1): (nowDate.getDate()+1));
var selDate = ""+
d.getFullYear()+
(d.getMonth()+1 < 10? "0"+(d.getMonth()+1): (d.getMonth()+1))+
(d.getDate()+1 < 10? "0"+(d.getDate()+1): (d.getDate()+1));
if(parseInt(nowDateParse) > parseInt(selDate)){
alert("You can only select dates in the future!");
$("#searchDate").attr("value", ""+
nowDate.getFullYear()+"-"
(nowDate.getMonth()+1 < 10? "0"+(nowDate.getMonth()+1): (nowDate.getMonth()+1))+"-"+
(nowDate.getDate()+1 < 10? "0"+(nowDate.getDate()+1): (nowDate.getDate()+1))
)
return false;
}
as you can see i have tired $("#searchDate").attr("value" as well as $("#searchDate").val( but both seem to cause the same problem.
So Date starts on the current date: 27/06/2016 (UK format for: 2016-06-27)
if the user selects a date before that E.G 26/06/2016 (UK format for: 2016-06-26)
it shows the alert but fails to change the date back to today's date.
I'm not against using Standard Javascript with out jQuery calls if thats what is needed.
NOTES:::
As a note this code looks quite complex due to the fact it's doing a date comparison and not a datetime comparison as Javascript's Date object is a datetime not just a date
Add this after the control of the date validity:
var dToday = new Date();
var day = ("0" + dToday.getDate()).slice(-2);
var month = ("0" + (dToday.getMonth()+1)).slice(-2);
today = dToday.getFullYear() + "-" + month + "-" + day;
$("#searchDate").val(today);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="date" id="searchDate">
You must create a new date object and set it as minDate when you initialize the datepickers
<label for="from">From</label> <input type="text" id="from" name="from"/> <label for="to">to</label> <input type="text" id="to" name="to"/>
var dateToday = new Date();
var dates = $("#from, #to").datepicker({
defaultDate: "+1w",
changeMonth: true,
numberOfMonths: 3,
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);
}
});
http://jsfiddle.net/dssoft32/qvmqawfd/
Related
I am using Modal for reminder date for task. The current date and the days before it has to be disabled so that the User can select the pick-up date starting next day inorder to Mark reminder with his specified task.
for this I am using input type :
<input type="datetime-local" class="form-control" autocomplete="off" id="actionreminderdate" />
If I use below java script it works for input type "Date" But not for Datetime-local.
$(function(){
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
/*alert(maxDate);*/
$('#actionreminderdate').attr('min', maxDate);
});
Can you help me with it?
You can set min and max values on the input field:
<input type="datetime-local" id="dateInput">
$(document).ready(function(){
elem = document.getElementById("dateInput")
var iso = new Date().toISOString();
var minDate = iso.substring(0,iso.length-1);
elem.value = minDate
elem.min = minDate
});
Get the current time in a recognised ISO format (you have to trim the zone data off the end, which is annoying), then set that as the input value and the input min.
https://jsfiddle.net/w8qgj543/24/
Im using angularjs for my project and bootstrap date-picker for select date.
In my project there is 2 input for select From date and To date. For the From date can select only today date, for the To date calculation i have add 1 year for from date using JavaScript and bind to To Date input. Data binding part is working fine. but when i click on To date its not show highlighted date in date-picker. Can i know how to highlight Today.
These are the option currently im used.
autoclose: true,
todayHighlight: true
In HTML You can use this.
<input name="dateInput" type="date" ng-model="TodayDate"/>
And In Controller you can get new Date(), So You can Assign the date to the ng-model.
var d = new Date();
var year = d.getFullYear();
var month = d.getMonth() + 1;
if (month < 10){
month = "0" + month;
};
if (day < 10) {
day = '0' + day;
}
$scope.TodayDate = year + "-" + month + "-" + day;
I am using Modal for reminder date for task. The current date and the days before it has to be disabled so that the User can select the pick-up date starting next day inorder to Mark reminder with his specified task.
for this I am using input type :
<input type="datetime-local" class="form-control" autocomplete="off" id="actionreminderdate" />
If I use below java script it works for input type "Date" But not for Datetime-local.
$(function(){
var dtToday = new Date();
var month = dtToday.getMonth() + 1;
var day = dtToday.getDate();
var year = dtToday.getFullYear();
if(month < 10)
month = '0' + month.toString();
if(day < 10)
day = '0' + day.toString();
var maxDate = year + '-' + month + '-' + day;
/*alert(maxDate);*/
$('#actionreminderdate').attr('min', maxDate);
});
Can you help me with it?
You can set min and max values on the input field:
<input type="datetime-local" id="dateInput">
$(document).ready(function(){
elem = document.getElementById("dateInput")
var iso = new Date().toISOString();
var minDate = iso.substring(0,iso.length-1);
elem.value = minDate
elem.min = minDate
});
Get the current time in a recognised ISO format (you have to trim the zone data off the end, which is annoying), then set that as the input value and the input min.
https://jsfiddle.net/w8qgj543/24/
I want to select any date and convert it to same date of next upcoming month. For example- I select 1st jun 2014 then it show 1st july 2014 and so on..
Fiddle
HTML
<input type='text' id='txtDate' readonly='true' />
<input type='button' id='btnConvert' value='Convert' /><br/>
Current Date : <span id='spnCurrentDate'></span><br/>
Next Month Date : <span id='spnNewDate'></span>
JS
$("#txtDate").datepicker({
changeMonth: true
});
$("#btnConvert").click(function(){
$("#spnCurrentDate").html($("#txtDate").val());
$("#spnNewDate").html($("#txtDate").val());
If you are just asking how to add one month to a Date object, you can't just add one to the month as 2014-01-31 + 1 month -> 2014-02-31 which is converted to 2014-03-03.
So if you roll over to the following month, you probably want to set the day to the last of the previous month:
function addOneMonth(date) {
var o = new Date(+date);
date.setMonth(date.getMonth() + 1);
// If have rolled over an extra month, set to last
// day of previous month
if (date.getDate() != o.getDate()) {
date.setDate(0);
}
return date;
}
addOneMonth(new Date(2014,0,31)); // 2014-02-28
or not…
DEMO here
var date1 = new Date();
date1.setMonth(date1 .getMonth() + 1);
now date1 is an object holding a date which is 1 months later
If you want to set it to the jQuery UI DatePickers, you can do this
$("#txtDate").datepicker({
changeMonth: true
});
$("#btnConvert").click(function(){
var date1 = new Date($("#txtDate").val());
date1.setMonth(date1 .getMonth() + 1);
$("#txtDate").datepicker("setDate", date1 );
});
Try This Demo : http://jsfiddle.net/PQfDc/151/
$("#txtDate").datepicker({
changeMonth: true,
minDate:'0',
onClose: function( selectedDate ) {
var d = new Date(selectedDate)
d.setMonth(d.getMonth() + 1);
$("#txtDate").datepicker("setDate", d );
$("#spnSelectedDate").text(selectedDate);
$("#spnNewDate").text($('#txtDate').val());
}
});
$("#txtDate1").datepicker({
changeMonth: true,
minDate:'0'
});
$("#btnConvert").click(function(){
var d1 = new Date($("#txtDate1").val());
var date = d1.getDate();
var month = d1.getMonth()+1;
var year = d1.getFullYear();
var newDate = month + "/" + date + "/" + year
$("#spnSelectedDate1").text($("#txtDate1").val());
$("#spnNewDate1").text(newDate);
});
This question already has answers here:
Disable certain dates from html5 datepicker
(6 answers)
Closed 9 years ago.
I am working on a website for booking movie tickets. What should I set the min attribute to, to prevent past dates in the HTML5 datepicker? (I do not want to use PHP solution mentioned here.)
Is there a pure HTML5/Javascript solution for this?
When the document loads have the date input disabled. Run an onload function that inserts today's date into the min field in the required format.
function onLoad() {
var input = document.getElementById("dateField");
var today = new Date();
// Set month and day to string to add leading 0
var day = new String(today.getDate());
var mon = new String(today.getMonth()+1); //January is 0!
var yr = today.getFullYear();
if(day.length < 2) { day = "0" + day; }
if(mon.length < 2) { mon = "0" + mon; }
var date = new String( yr + '-' + mon + '-' + day );
input.disabled = false;
input.setAttribute('min', date);
}
document.addEventListener('load', onLoad, false);
<body>
<input id="dateField" type="date" disabled />
</body>
Here it is in a fiddle: http://jsfiddle.net/tj9Xh/2/
try this:
<input id="dateField" type="date"/>
var dt= new Date();
var yyyy = dt.getFullYear().toString();
var mm = (dt.getMonth()+1).toString(); // getMonth() is zero-based
var dd = dt.getDate().toString();
var min = yyyy +'-'+ (mm[1]?mm:"0"+mm[0]) +'-'+ (dd[1]?dd:"0"+dd[0]); // padding
alert(min);
$('#dateField').prop('min',min);