How to display the start date one day ahead with bootstrap-datepicker - javascript

The select date seem to be empty when I added startDate: '+1d' into the javascript. Otherwise, it is just displaying today's date. I need it to display one day ahead and in some situations, two days ahead as well.
Javascript below:
$("#datepicker").datepicker({
autoclose: true,
todayHighlight: false,
startDate:'+1d',
dateFormat: 'dd-mm-yy'
}).datepicker('update', new Date());
HTML Code below:
<div class="form-group">
<div class="col-sm-2"></div>
<label for="inputName" class="col-sm-3 control-label">Select Date Range</label>
<div class="col-sm-4">
<div class='col-sm-6'>
<div class="form-group">
<div id="datepicker" class="input-group date" data-date-format="dd-mm-yyyy">
<input class="form-control" type="text" readonly id="start"/>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
<div class='col-sm-6'>
<div class="form-group">
<div id="datepicker2" class="input-group date" data-date-format="dd-mm-yyyy">
<input class="form-control" type="text" readonly id="end"/>
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
</div>
</div>

you can use moment.js for this type of date/time manipulation
var tomorrow = moment().add(1, 'day').format('DD/M/YYYY');
outputs
03/11/2016
see jsbin:
here is the full working jsfiddle: https://jsbin.com/haxamaxaqe/edit?html,output

$('#txtDate').datepicker('setDate', "+1d");

Related

disable past date after first date is selected datepicker

i'm working to make a simple booking form that redirect to an staah booking engine using GET (they dont have any apis or embedded widget).
the booking engine only accept "dd M yyyy" date format (eg 19 Apr 2018)
i have search SO but dont find any working solution, recently i found working fiddle and i have edit that fiddle for my need (you would know what i want to achieve in the fiddle). you can see it here : https://jsfiddle.net/k5zookLt/1929/
but the problem is they have a different approach on the HTML code and i have tried to modifying the js code for my html but it doesn't work. i dont have any skill in javascript. i dont know why this fiddle doesn't work in my html code
my code
<div class="col-md-2 col-sm-3">
<div class="input-group date" >
<input type="text" class="jaraksmall sm-form-control form-control from " id="from" name="from" required>
</div>
</div>
<div class="col-md-2 col-sm-3">
<div class="input-group date">
<input type="text" class="jaraksmall sm-form-control form-control to" id="to" name="to" required>
</div>
fiddle html code :
<div class="line col-sm-4">
<div class="form-group">
<label>First check in:</label>
<input type="text" class="form-control form-control-1 input-sm"placeholder="CheckIn" >
</div>
<div class="form-group">
<label>First check out:</label>
<input type="text" class="form-control form-control-2 input-sm" placeholder="CheckOut">
</div>
</div><!--line-->
how to make the js code from fiddle working well in my html code?
sorry for my bad english
That's because selector for .each and selector to find input element is not finding the required element. You need to :
1) to remove each statement.
2) modify datepicker input element selectors to use classes that you have added(to and from).
var startDate = new Date();
var fechaFin = new Date();
var FromEndDate = new Date();
var ToEndDate = new Date();
$(".from").datepicker({
autoclose: true,
startDate: "+0d",
format: 'd M yyyy'
}).on('changeDate', function(selected){
startDate = new Date(selected.date.valueOf());
startDate.setDate(startDate.getDate(new Date(selected.date.valueOf()))+1);
$('.to').datepicker('setStartDate', startDate);
});
$('.to').datepicker({
autoclose: true,
format: 'd M yyyy'
}).on('changeDate', function(selected){
FromEndDate = new Date(selected.date.valueOf());
FromEndDate.setDate(FromEndDate.getDate(new Date(selected.date.valueOf())));
$('.from').datepicker('setEndDate', FromEndDate);
});
Working Demo
You can also replace the html code as below
<div class="line" style="width:100%">
<div class="col-md-2 col-sm-3" style="display: inline-block">
<div class="input-group date" >
<input type="text" class="jaraksmall sm-form-control form-control-1 form-control from " id="from" name="from" required>
</div>
</div>
<div class="col-md-2 col-sm-3" style="display: inline-block">
<div class="input-group date">
<input type="text" class="jaraksmall sm-form-control form-control-2 form-control to" id="to" name="to" required>
</div>
</div>
</div>
jsfiddledemo

How to use linked DateTime using Bootstrap DateTimePicker?

I have a form which contains Departure date and return date. And I want to set the minimum date in the return date field is the date of selected departure date. I already put the id in div tag or input tag, but nothing worked. Do you know why?
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Departure<span class="required"> *</span></label>
<div class="input-group date form_datetime col-md-4 col-sm-4 col-xs-12" id="res_departure1" data-link-field="dtp_input1">
<input class="form-control pull-right" size="16" type="text" value="" readonly required>
<input type="hidden" id="dtp_input1" name="res_departure" value="" />
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Return<span class="required"> *</span></label>
<div class="input-group date form_datetime col-md-4 col-sm-4 col-xs-12" id="res_return1" data-link-field="dtp_input2">
<input class="form-control pull-right" size="16" type="text" value="" readonly required>
<input type="hidden" id="dtp_input2" name="res_return" value="" />
<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
</div>
</div>
This is my js
$('.form_datetime').datetimepicker({
//language: 'fr',
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
startDate: new Date
});
$("#res_departure1").on("dp.change", function (e) {
$('#res_return1').data("DateTimePicker").setMinDate(e.date);
});
$('#res_departure1').datetimepicker().on('changeDate', function(ev) {
var departureDate = ev.date;
$('#res_return1').datetimepicker('setStartDate', departureDate);
});
$('#res_return1').datetimepicker().on('changeDate', function(ev) {
var returnDate = ev.date;
$('#res_departure1').datetimepicker('setEndDate', returnDate);
});
Because when you are talking about date only, then better to use datepicker on jQuery ui library, and then you can use the onSelect event of the departure date and then set the min date of return field.
onSelect: function(selectedDate)
{
$('#return').datepicker('option', 'minDate', selectedDate);
}
you can check the working fiddle here.
Try this instead of your code
$('.form_datetime').datetimepicker({
//language: 'fr',
format: "yyyy-mm-dd hh:ii",
autoclose: true,
todayBtn: true,
startDate: new Date()
});
$("#res_departure1").on("dp.change", function (e) {
$('#res_return1').data('DateTimePicker').minDate(new Date(e.timeStamp));
});

Linked Datepicker

I am new to bootstrap and trying to use linked datepicker where
Only Date should be displayed (not time).
Default Selected Date shown in Datepicker as well as in textbox should be today's date on page load (before selecting any date from datepicker default date appeared in text box should be today's date).
Future dates should be disabled in startdate and enddate.
I am using following code :
Html Code
<div class="container">
<div class="col-sm-6" style="height:75px;">
<div class='col-md-5'>
<div class="form-group">
<div>Start</div>
<div class='input-group date' id='startDate'>
<input type='text' class="form-control" name="startDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class='col-md-5'>
<div class="form-group">
<div>End</div>
<div class='input-group date' id='endDate'>
<input type='text' class="form-control" name="org_endDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
Script
jQuery(function () {
jQuery('#startDate').datepicker({
//format: 'mm-dd-yyyy',
endDate: '+0d',
autoclose: true
});
jQuery('#endDate').datepicker({
//format: 'mm-dd-yyyy',
endDate: '+0d',
autoclose: true,
useCurrent: false
});
jQuery('#startDate').datetimepicker().datepicker("setDate",new Date());
jQuery('#endDate').datetimepicker().datepicker("setDate",new Date());
jQuery("#startDate").on("dp.change",function (e) {
jQuery('#endDate').data("DateTimePicker").setMinDate(e.date);
});
jQuery("#endDate").on("dp.change",function (e) {
jQuery('#startDate').data("DateTimePicker").setMaxDate(e.date);
});
});
Using jQuery('#startDate').datepicker().datepicker("setDate",new Date());
I can achieve point no. 2.
However, issue is future date disabled works for datepicker and startdate enddate selection works with datetimepicker. (Using datepicker and datetimepicker shows two datepikcer controls one on textbox other on addon)
How to combine this to achieve all the three point using only datepicker
I think that you are mixing two different libraries eonasdan datetimepicker and bootstrap datepicker.
If you want to use the first library, then you have to:
specify format option according momentjs docs
use minDate and maxDate properties and methods
listen for dp.change event
use useCurrent option
Here a working example:
jQuery('#startDate').datetimepicker({
format: 'MM-DD-YYYY',
maxDate: moment().endOf('day'),
defaultDate: moment()
}).on("dp.change",function (e) {
jQuery('#endDate').data("DateTimePicker").minDate(e.date);
});
jQuery('#endDate').datetimepicker({
format: 'MM-DD-YYYY',
maxDate: moment().endOf('day'),
defaultDate: moment()
}).on("dp.change",function (e) {
jQuery('#startDate').data("DateTimePicker").maxDate(e.date);
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.0/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<div class="container">
<div class="col-sm-6" style="height:75px;">
<div class='col-md-5'>
<div class="form-group">
<div>Start</div>
<div class='input-group date' id='startDate'>
<input type='text' class="form-control" name="startDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class='col-md-5'>
<div class="form-group">
<div>End</div>
<div class='input-group date' id='endDate'>
<input type='text' class="form-control" name="org_endDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
If you want to use the bootstrap datepicker, then you have to:
specify format option lowercase
use endDate option and setStartDate and setEndDate functions
listen for changeDate event
use update method to set default date
Here a working example:
jQuery('#startDate').datepicker({
format: 'mm-dd-yyyy',
endDate: '0d',
autoclose: true
}).on("changeDate",function (e) {
jQuery('#endDate').datepicker("setStartDate", e.date);
});
var d = new Date();
var now = new Date(d.getFullYear(), d.getMonth(), d.getDate());
$('#startDate').datepicker('update', now);
jQuery('#endDate').datepicker({
format: 'mm-dd-yyyy',
endDate: '0d',
autoclose: true
}).on("changeDate",function (e) {
jQuery('#startDate').datepicker("setEndDate", e.date);
});
$('#endDate').datepicker('update', now);
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<div class="container">
<div class="col-sm-6" style="height:75px;">
<div class='col-md-5'>
<div class="form-group">
<div>Start</div>
<div class='input-group date' id='startDate'>
<input type='text' class="form-control" name="startDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
<div class='col-md-5'>
<div class="form-group">
<div>End</div>
<div class='input-group date' id='endDate'>
<input type='text' class="form-control" name="org_endDate" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
</div>
</div>
</div>
Note that, according the docs, update accepts a Date object but $('#startDate').datepicker('update', new Date()); did not work for me (and I don't know why), while the following lines set a default date correctly:
var d = new Date();
var now = new Date(d.getFullYear(), d.getMonth(), d.getDate());
$('#startDate').datepicker('update', now);

Datepicker : Datepicker on Modal (z-index)

I face this problem today when I try to show datepicker on Bootstrap Modal.
I'm using bootstrap-datepicker as datepicker library.
Here is my modal form :
<div class="form-group">
<label for="message-text" class="control-label">Start Date</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control pull-right datepicker" id="datepicker2" name="start_date">
</div>
</div>
And here is my datepicker script :
$('#datepicker2').datepicker({
autoclose: true,
format: 'yyyy-mm-dd',
zIndexOffset: 10000
});
I already using zIndexOffset but it's not working, the calendar still shown behind the modal.
Can anyone suggest the solution ?
Thank you.
You should try adding the date picker container inside the modal div. You could give the modal an id like this:
<div class="form-group" id="myModalWithDatePicker">
<label for="start_date" class="control-label">Start Date</label>
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="text" class="form-control pull-right datepicker" id="datepicker2" name="start_date">
</div>
</div>
Then you can use the container option like the following:
$('#datepicker2').datepicker({
autoclose: true,
container: '#myModalWithDatePicker',
format: 'yyyy-mm-dd'
});
I used this option to fix.
.modal-open .ui-datepicker{z-index: 2000!important}
this option has been applied when modal open (class modal-open added to body)

Setting mindate in datepicker on the bases of another datetimepicker selected date

I want to set mindate from another datetimepicker selected date in bootstrap datepicker. Is it possible to do this??
<div class="form-group hide" id="divStartsOn">
<div class="col-md-3">
<label>Start On</label>
</div>
<div class="input-group col-md-9" style="padding-left: 14px;">
<input id="datetimepicker8" type="text" class="date-picker form-control" />
<label for="datetimepicker8" class="input-group-addon btn">
<span class="glyphicon glyphicon-calendar"></span>
</label>
</div>
<div class="clearfix"></div>
</div>
<div class="input-group col-md-6" style="padding-left: 14px;">
<input id="datetimepicker9" type="text" class="date-picker form-control" />
<label for="datetimepicker9" class="input-group-addon btn">
<span class="glyphicon glyphicon-calendar"></span>
</label>
</div>
jQuery Code:
$("#datetimepicker6").on("dp.change", function (e) {
var sdate = $('#txtdatetimepicker1').val().split('/');
sdate[2] = sdate[2].substring(0, 4);
var stdate = sdate[0] + "/" + sdate[1] + "/" + sdate[2];
$("#datepicker8").datepicker("option", { minDate: stdate })
});
I solved this is by removing datpicker.js and add jquery.ui.js after jquery.js

Categories