Why datepicker calendar will not giving the assigned value? - javascript

Here in the html I'm assigning the date to the text field but the calendar of the datepicker will not pick that assigned value it will show always me the current date.
Assigning the value to the text field Using jQuery:-
$('#datepicker').val("06-10-2018");
$( function() {
$( "#datepicker" ).datepicker({
numberOfMonths: 2,
onSelect: function(selected) {
$("#txtFromDate").datepicker("option","maxDate", selected)
}
});
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>
How will change the current value of calendar according the assigning of the value to the text field. Can anybody will help me for this. Thank you.

You should initialize the datepicker first then set default value like this:
var myDate = "06/10/2018";
$("#datepicker").datepicker({
minDate: myDate
}).datepicker("setDate", myDate);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<p>Date: <input type="text" id="datepicker"></p>

If you would like to set value of the input field and update calender. Then you should use update option of bootstrap-datepicker.
$('#datepicker').val("06-10-2018");
$('#datepicker').datepicker("update");

Related

jquery timepicker not working with datepicker

I am installing jquery datepicker and timepicker for the first time. Datepicker is working but timepicker is not. Maybe some js conflict?
Datepicker files
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="css/date.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
Timepicker and using this script https://plugins.jquery.com/jt.timepicker/
<link rel="stylesheet" type="text/css" href="css/jquery.timepicker.css">
<script type="text/javascript" src="js/jquery.timepicker.min.js"></script>
Other js on page before closing body tag
<script type="text/javascript" src="///js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/jquery.magnific-popup.js"></script>
<script type="text/javascript" src="js/owl.carousel.js"></script>
<script type="text/javascript" src="js/jquery.countTo.js"></script>
<script type="text/javascript" src="js/jquery.appear.js"></script>
<script type="text/javascript" src="js/custom.js"></script>
$( function() {
$( "#datepicker" ).datepicker({
yearRange: '-120:+0',
dateFormat: 'dd-mm-yy',
changeMonth: true,
changeYear: true
});
} );
</script>
<script>
$('.timepicker').timepicker();
</script>
Is there anyway I can limit datepicker to today() as I am using it for birthdate.
Is there any way to output error in jquery.
And what is wrong with timepicker. Nothing happens when I click the form field.
I changes timepicker js to jquery.timepicker.js
Added maxDate: '0' to limit date to today's date.
Used Console to find jquery errors.

on fire thrice because of the script

whenever i tried to select a date the on function is fired 3 times i tried removing this script
and it does work, but the ui is ruined and also i noticed that onselect is not working.
<!Doctype html>
<html>
<head>
<!-- <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<script>
$(document).ready(function(){
var newdate = new Date();
$("#inputMultiDate").datepicker({
autoclose: true,
todayHighlight: true,
multidate: true,
format: 'dd-mm-yyyy',
onSelect: function(date){
console.log(date);
}
}).on("change", function() {
var dates = $('#inputMultiDate').val().split(',');
alert(dates[0]);
});;
});</script>
</head>
<body>
<input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">
</body>
</html>
onSelect is not working because onSelect is an option of jQuery UI datepicker, however you're using Bootstrap datepicker, which is a very different plugin.
Instead, boostrap datepicker uses events chained to the datepicker itself, like you have done with your change. However, given how the datepicker actually works behind the scenes, I'd imagine it changes the input multiple times, hence firing multiple alerts.
Instead of change, try changeDate:
var newdate = new Date();
$("#inputMultiDate").datepicker({
autoclose: true,
todayHighlight: true,
multidate: true,
format: 'dd-mm-yyyy'
}).on("changeDate", function() {
var dates = $(this).val().split(',');
console.log(dates[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/css/datepicker.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<input type="text" id="inputMultiDate" class="form-control" placeholder="Pick the multiple dates">

how to create jquery ui date picker current month and date and year show only

how to create jquery ui date picker current month and date and year show only
<html>
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function()
{
$( "#datepicker" ).datepicker({
changeMonth: false,
changeYear: false,
});
});
</script>
</head>
<body>
<div class="row">
<label for="email">Check Date:</label><br />
<input id="datepicker" class="input" name="date" type="text" value="" required/><br />
</div>
</body>
</html>
tell me how to disable all and enable only current date time and year
try this:
$( "#datepicker" ).datepicker( "option", "disabled", true );
but my suggestion if you don't want user to change it then instead of datepicker just have div and show the current date.

How to show date picker value in a label and textbox

$("#datepicker").datepicker();
I am using this function
and its box of input is
<input type="text" id="datepicker" name="fulldate" class="form-control wid-100"> <span id="#dpicker_name"></span>
Please help me in it friends
You have to do like
$(document).ready(function(){
$("#datepicker").datepicker({
onSelect: function(dateText, inst) {
$("#dpicker_name").html(dateText);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css">
<input type="text" id="datepicker" name="fulldate" class="form-control wid-100">
<span id="dpicker_name"></span>
Please check Documentation
Also you can get it without onSelect like
$("#datepicker").datepicker("getDate");
Try this
$( "#datepicker" ).datepicker({
showOn: "button",
buttonImage: "images/calendar.gif",
buttonImageOnly: true,
buttonText: "Select date"
});
Seems to me you want to trigger the datepicker on click of icon. Check the docs here .
used to this code
Demo
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
$(document).ready(function(){
$('#datepicker').on('change', function(){
var dateThis = $(this).val();
$('#dpicker_name').text(dateThis);
});
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"> <span id="dpicker_name"></span></p>
</body>
</html>
Try this.
$("#dpicker_name").text($( "#datepicker" ).datepicker( "getDate" ));
Get value in javascript as
var date = $(".datepicker").val();
and assigned it to the lable

datepicker no past dates

i need guide on how to apply this restriction for back dates for the date picker as i am new to this javascript.
<script type="text/javascript" src="javascript/datepickr.min.js"></script>
<input class="" type="Text" size="30" name="Date" id="Date" placeholder="yyyy-mm-dd"/>
<script type="text/javascript">
new datepickr('Date',
{
'dateFormat': 'Y-m-d'
}
);
</script>
i do saw some people post about solution like this code, but i don't know how to apply in my code
$(function() {
$( "#datepicker" ).datepicker({ minDate: 0});});
i have tried this as well, still not working, and it is more worst as the date picker can't be show
<link rel="stylesheet" href="css/jquery-ui-1.9.2.css" type="text/css" />
<link rel="stylesheet" href="style/jquery-ui-1.9.2.min.css" type="text/css" />
<script type="text/javascript" src="javascript/jquery-ui-1.9.2.js"></script>
<script type="text/javascript" src="javascript/jquery-ui-1.9.2.min.js"></script>
<script type="text/javascript" src="javascript/jquery-1.8.3.js"></script>
<input type='text' id='datepicker'></input>
<script type="text/javascript">
$("#date").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "-35:+0",
dateFormat: "yy-mm-dd",
minDate:mdate() });
function mdate(){
var str = "-0Y";
return str; }
</script>
Take look at this. In it I have used JQuery UI 1.9.2
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<input id="datepicker" type="text" />
<script>
$( "#datepicker" ).datepicker({
minDate: 0
});
</script>
I am setting minDate on load. Use this code and this will work for you. Click Run Code Snippet code if you want to see the code in action.
$( "#datepicker" ).datepicker({
minDate: 0
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<input id="datepicker" type="text" />

Categories