How to disable past days and weekends in Jquery - javascript

Does anybody know why this Script not working? I am trying to disable weekends and previous dates? I have tried searching all over the web but I am struggling.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<input type="date" id="date" class="form-control">
<script type="text/javascript">
$(function() {
$( "#date" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
minDate : 'now'
});
});
</script>
DatePicker

According to documentation, you simply need to disable the days of the week by utilizing numbers 0-6. Sunday starting at 0 and Saturday ending at 6, and obviously, every other day of the week assigned accordingly. Also, the format of daysOfWeekDisabled is a comma delimited string which you will see below. In order to only show today's date and on, simply set a minDate of today's date. All of the above is implemented in code below:
let dateToday = new Date();
$('#date').datepicker({
daysOfWeekDisabled: '0, 6',
minDate: dateToday
})

In your case, the problem is you have input type as date but if you change that to test it will work.
Working Example:
<html xmlns="http://www.w3.org/1999/xhtml">
   <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" />
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<head>
<script type="text/javascript">
$(function () {
$("#date").datepicker({
beforeShowDay: $.datepicker.noWeekends,
minDate : 'now'
});
});
</script>
</head>
<body>
<input type="text" id="date" />
</body>
</html>

Related

How can I change my date picker into a year only picker?

I want to change to year picker calendar only in a dynamic input fields.The calender should show only year values Can any one help me please.
Here is the javascript code.
$('body').on('focus',".datepicker", function(){
if( $(this).hasClass('hasDatepicker') === false ) {
$(this).datepicker();
}
});
You can try this one. And see if it will help your problem.
HTML:
<select name="yearpicker" id="yearpicker"></select>
JS:
var startYear = 1800;
for (i = new Date().getFullYear(); i > startYear; i--)
{
$('#yearpicker').append($('<option />').val(i).html(i));
}
Here is a jsfiddle link
$('body').on('focus',".datepicker", function(){
if( $(this).hasClass('hasDatepicker') === false ) {
$(this).datepicker({
minViewMode: 2,
format: 'yyyy'
});
}
});
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/css/bootstrap-datepicker.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.5.0/js/bootstrap-datepicker.js"></script>
<input type="text" class="datepicker" />
I assume it is bootstrap datepicker (Link)
I would not use a datepicker if you want to select a year only. Use a select for that. However, you can modifiy the datepicker (since you didn't mention which one you are using I assume jQuery UI) to only return the year.
Have a look at this fiddle:
$(function() {
$(".datepicker").datepicker({ dateFormat: 'yy' });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="https://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<input type="text" class="datepicker"/>

disabling dates in the past bootstrap

Please give me the easiest way to disabling dates in the past bootstrap
<script type="text/javascript">
$(function(){
$('.datepicker').datepicker();
});
</script>
Date: <input type="text" class="datepicker"></input>
I have included bootstrap datepicker.css, bootstrap-datepicker.js, bootstrap.min.css and jquery-1.11.1.js files
Use the startDate option.
<script type="text/javascript">
$(function(){
$('.datepicker').datepicker({
startDate: new Date(),
});
});
</script>
Or you can simply use '+0d'
<script type="text/javascript">
$(function(){
$('.datepicker').datepicker({
startDate: '+0d',
});
});
</script>
This will start the calender from +0 days(i.e today)

JavaScript date issue should display error for invalid date

I am writing an html file to take date input. I am using date picker to select the date, instead of selecting date from the date picker, if the user is manually typing the date in text field for ex: 09/31/16 I need to give information to the user that date is invalid date. But my code given below is taking as october 1st 2016 when I created the date object. if I am converting date to ISOString it is displaying as 30th sept. But my desired output is to display the message to the browser as an "Invalid date". Please help me as I have to resolve this because I am going to use dates at many places during my development. Thank You.
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(function () {
$("#datepicker").datepicker();
});
function verifyDate() {
var d = new Date($("#datepicker").val());
console.log(d);
var d = new Date($("#datepicker").val()).toISOString();
console.log(d);
}
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" onblur="verifyDate()"></p>
</body>
</html>
if you want to get the date on user selection, your code should be like:
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script>
$(function() {
$("#datepicker").datepicker({
onSelect: function(){
var dateValue = $(this).datepicker('getDate');
console.log(dateValue);
}
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker" ></p>
</body>
Following way of validating dates can help in this:
let dateObj = document.getElementById("datepicker");
if (dateObj.validity.badInput) {
// show error message
}

jquery open calendar to specific month

On click of textbox, a calendar opens with current month. I want to open the calendar to specific date.Currently, the calendar opens opens to current month view. Can some one please help me with this? Thanks!
Select Date: <input type="text" id="datepicker"/>
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: enableAllTheseDays,
onSelect: function (date, inst) {
//MyLogic
}
});
You can use the defaultDate option to open to a specific date. Let's assume you want it to open to July 1st, 2014:
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: enableAllTheseDays,
defaultDate: new Date(2014, 6, 1)
onSelect: function (date, inst) {
//MyLogic
}
});
The format for date is year/month/day. Note: for the month, it is month - 1. So January (1st month) would be 0 and February (2nd month) would be 1.
Alternatively, you can also specify the same date like so:
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: enableAllTheseDays,
defaultDate: new Date('1 July 2014')
onSelect: function (date, inst) {
//MyLogic
}
});
You can also define the defaultDate like so:
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yy',
beforeShowDay: enableAllTheseDays,
defaultDate: new Date('7/1/2014')
onSelect: function (date, inst) {
//MyLogic
}
});
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<style>
table {
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
}
</style>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
<script>
$(document).ready(function(){
$(function() {
$( "#datepicker" ).datepicker({
defaultDate: '-2m'
});
});
});
</script>
</body>
</html>

change the second datepickers minDate when selecting date from another datapicker

I' m using jquery ui datepicker.
I have two datepicker on the page, from and to datepickers
when i select a date in one datepicker i update the max and min date to the second datepicker.
my problem is that just before it close(hide) the seleted datepicker,
it show the second datepicker instead of the first one (just when i update the min date of the second datepicker) and then close it.
why does it show the second datepicker when i update the min date from the other datepicker?
how can i hide the datepicker before i update the second datepicker?
<!doctype html>
<html >
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>
<input name="fromDateCalendar" type="text" value="10/12/2013" id="fromDateCalendar" style="width:100px;" />
<input name="toDateCalendar" type="text" value="09/01/2014" id="toDateCalendar" style="width:100px;" />
<script>
$(function () {
$('#fromDateCalendar').datepicker({
onSelect: function (date) {change1(date);}});
$("#toDateCalendar").datepicker({
onSelect: function (date) {change2(date);}, maxDate: '0' });
});
function change1(date) {
$('#toDateCalendar').datepicker('option', 'minDate', date);}
function change2(date) {
$('#fromDateCalendar').datepicker('option', 'maxDate', date);}
</script>
</body>
</html>
to solve the problem of showing the #2 datepicker for a moment(in the place of the #1 datepicker) when i change the max date,
i call the ....datepicker('option', 'minDate', start) after the #1 datepicker close with setTimeout
onSelect: function (date) {
setTimeout(function () {
....datepicker('option', 'minDate', start)
}, 300)
}....

Categories