Once the user has selected the date from the datepicker, i want to add 41 days.
<script language="javascript">
$(document).ready(function() {
$("#startdate").datepicker({ dateFormat: 'd/m/y'});
$('#startdate').datepicker({
onSelect: function(dateStr) {
var nights = parseInt($('#numofdays').val());
var depart = $.datepicker.parseDate('d/m/y', dateStr);
depart.setDate(depart.getDate('d/m/y') + nights);
$('#calc').val(depart);
}
});
});
</script>
Start: <input type="text" id="startdate" class="datepicker"><br />
<input type="hidden" id="numofdays" value="41"><br />
Calc: <input type="text" id="calc">
You might just need to combine the options in the same initiating function:
$("#startdate").datepicker({
dateFormat: 'd/m/y',
onSelect: function(dateStr, inst) {
var nights = parseInt($('#numofdays').val());
var depart = $.datepicker.parseDate('d/m/y', dateStr);
depart.setDate(depart.getDate('d/m/y') + nights);
$('#calc').val(depart.toLocaleDateString());
}
});
See this in action: http://jsfiddle.net/william/L9Szd/.
$('#startdate').datepicker('getDate') will return a Date object
d = $('#startdate').datepicker('getDate');
d.setDate(d.getDate()+nights); // add int nights to int date
alert(d);
Adding d.getDate() and nights will jump time forward. So if 9/16/2011 + 41 days you'll get 10/25/2011
would seem strange as a user to select a date, and then the box says a date that's 41 days later instead of the one I picked. why not do this addition server side with PHP?
$date_string = date('Y-m-d', strtotime('+41 days', strtotime($_POST['date_input'])));
This is the only simple way I found to do this:
To set date to 41 days ahead of the selected date:
1.Fetch selected date from a datepicker field
var newdate = new Date($("#datepicker_date_field_1").datepicker("getDate"));
2.Increase the date got from above
newdate.setDate(newdate.getDate() + 41);
3.Assign the newly formed date to another (or same) datepicker field
$("#datepicker_date_field_2").datepicker("setDate",newdate);
Related
how can I remove days from input type date? I added default data:
<input required class="data_inizio" type="date" name="inizio" value="2018-01-31">
But I would to remove previous days before that..
You could use the min attribute, to only allow future dates. Like so: <input required class="data_inizio" type="date" min="2018-01-31" name="inizio" value="2018-01-31">. You will only need to replace the min value with a dynamic value which represents the current date. You could use something like php for that: .
So it would become:
<input required id="date-input" class="data_inizio" type="date" min="<?=date('Y-m-d', time())?>" name="inizio" value="2018-01-31">`.
You can find the browser support over here: Browser_compatibility
Or setting the date using javaScript:
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = yyyy + '-' + mm + '-' + dd;
document.getElementById("date-input").setAttribute('min', today);
I use the popular JQuery UI Datepicker to add/modify events on my web site. I am having a problem with getting the date to show up right using setDate. The incorrect date seems to always show up no matter what I do. Here are some various things I have tried:
The date shows up as 01/17/1970 when it should be 10/15/2015, this is how I need it to work, with the php:
$(function() {
var date = new Date(<?php echo strtotime($date); ?>);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The date has inconsistent values when done like this, the date will always be wrong no matter what date you set, but there is no pattern.
$(function() {
var date = new Date(2015, 10, 15);
$("#date").datepicker();
$("#date").datepicker("setDate", date);
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
The Javascript Date object's constructor takes milliseconds when given only one argument.
The PHP function strtotime() returns the Unix Timestamp, the number of seconds since 1970-01-01.
If you multiply the timestamp by 1,000, you'll get a suitable value to pass to the Date constructor.
var date = new Date(<?php echo (strtotime($date) * 1000); ?>);
I had also tried it like this previously, but forgot to put quotation marks around the php. Now it works:
<script>
$(function() {
$("#date").datepicker();
$("#date").datepicker("setDate", "<?php echo $date ?>");
$("#date").datepicker( "option", "showAnim", "slideDown" );
});
</script>
i have used a date picker from bootstrap. I was wondering how would i exclude the 10 days from the selected day the user has clicked
for example the user chooses 10th April 2015. That user can only select dates from 10th April 2015 - 19th April 2015 which would be the ending date.
Here is my code for the date picker.
<script type="text/javascript">
var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
$(function () {
$('#startdate').datetimepicker({
minDate:today,
format: 'YYYY-MM-DD HH:MM:SS'
}).change(function (selected) {
var startDate = new Date(selected.date.valueOf());
$('#enddate').datetimepicker('maxDate', startDate+10);
});
$('#enddate').datetimepicker({
minDate:today,
format: 'YYYY-MM-DD HH:MM:SS'
});
});
</script>
Option "enabledDates", disables selection of dates NOT in the array, perhaps that will work for you?
http://eonasdan.github.io/bootstrap-datetimepicker/Options/#enableddates
This question already has answers here:
How to change date format (MM/DD/YY) to (YYYY-MM-DD) in date picker
(18 answers)
Closed 8 years ago.
I am working with jQuery date picker, and the problem is the format of the date, i want the format same as the MySQL date format as i do not get the correct value of the date in MySQL table, i am pasting the code below.
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('#pdate').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate)
});
});
</script>
Form
<input type="text" name="date" title="" placeholder="mm / dd / yyyy" class="date" id="pdate" required="required"/>
Currently the date picker's date format is 05/16/2014 and MySQL's date format is MM-DD-YYYY
do like this
$("#pdate").datepicker({showOn: 'focus',changeMonth: true,
changeYear: true,dateFormat: 'mm-dd-yy',
minDate: new Date(currentYear, currentMonth, currentDate)
});
You need to add dateFormat attribute to your datepicker() function.
for more info check here
add dateFormat to your datepicker
$('#pdate').datepicker({
minDate: new Date(currentYear, currentMonth, currentDate),
dateFormat: 'mm-dd-yy'
});
I have the following code to set the minimum date of datepicker.
$( "#sd" ).datepicker({
// before datepicker opens run this function
beforeShow: function(){
// this gets today's date
var theDate = new Date();
// sets "theDate" 2 days ahead of today
theDate.setDate(theDate.getDate() + 2);
// set min date as 2 days from today
$(this).datepicker('option','minDate',theDate);
},
// When datepicker for start date closes run this function
onClose: function(){
// this gets the selected start date
var theDate = new Date($(this).datepicker('getDate'));
// this sets "theDate" 1 day forward of start date
theDate.setDate(theDate.getDate() + 1);
// set min date for the end date as one day after start date
$('#ed').datepicker('option','minDate',theDate);
}
});
If today's date is 11/14/2012 then the value of datepicker is 11/16/2012. However, since the date should be based on server's datetime I cannot use javascript date because it will get the current date of the computer instead of server's date.
So what I did is change it to the following code:
$( "#sd" ).datepicker({
// before datepicker opens run this function
beforeShow: function(){
// this gets today's date
var theDate = new Date();
// sets "theDate" 2 days ahead of today
//theDate.setDate(theDate.getDate() + 2);
theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);
// set min date as 2 days from today
$(this).datepicker('option','minDate',theDate);
},
// When datepicker for start date closes run this function
onClose: function(){
// this gets the selected start date
var theDate = new Date($(this).datepicker('getDate'));
// this sets "theDate" 1 day forward of start date
theDate.setDate(theDate.getDate() + 1);
// set min date for the end date as one day after start date
$('#ed').datepicker('option','minDate',theDate);
}
});
Please take a look at this line:
theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);
BTW, I am using codeigniter session.
This works great but the problem is the minimum date in datepicker is not set to 11/16/2012 instead it was set to 11/2/2012.
Anyone know how to insert the PHP value correctly into jQuery?
I have already found the answer. Here it is:
$( "#sd" ).datepicker({
// before datepicker opens run this function
beforeShow: function(){
// this gets today's date
var theDate = new Date();
theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>);
// set min date as 2 days from today
$(this).datepicker('option','minDate',theDate);
},
// When datepicker for start date closes run this function
onClose: function(){
// this gets the selected start date
var theDate = new Date($(this).datepicker('getDate'));
// this sets "theDate" 1 day forward of start date
theDate.setDate(theDate.getDate() + 1);
// set min date for the end date as one day after start date
$('#ed').datepicker('option','minDate',theDate);
}
});
It should extract the day only and not the whole date.
theDate.setDate(<?php echo date('d', strtotime($this->session->userdata('checkin'))) ?>);
Instead of
theDate.setDate(<?php echo $this->session->userdata('checkin') ?>);