air-datepicker js : hour+2 - javascript

I try to set hours (+2h) to actual day and not for the others.
Example:
Today is October 18th, time: 10am.
If I click (without changing the date) -> Time is 12pm.
If I click and choose tomorrow (19th October) -> I choose any time.
Today is October 19th, time: 08am.
If I click (without changing the date) -> Time is 10am.
If I click and choose tomorrow (20th October) -> I choose any time.
PS : If possible, I want to disable the entry in the input (because we can change the time)
Thx
var start = new Date()
var hour = start.getHours()+2;
$('#date').datepicker({
language: 'en',
timepicker: true,
minHours: hour
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/i18n/datepicker.en.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.css" rel="stylesheet"/>
<input type='text' id="date" class="" />

EDIT: I thought you want to disable previous dates (everything < now + 2hours). If not, just change minDate: start to startDate: start.
Try with:
var start = new Date();
// first available date and time
start.setHours(start.getHours() + 2);
$('#date').datepicker({
language: 'en',
timepicker: true,
minDate: start,
onSelect: function(fd, d, picker) {
if (d < start) {
return;
}
if (d.getDate() == start.getDate() &&
d.getMonth() == start.getMonth() &&
d.getYear() == start.getYear()) {
// set minHours to start hours
picker.update({
minHours: start.getHours()
});
} else {
// set minHours to 0 hours
picker.update({
minHours: 0
});
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/i18n/datepicker.en.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.css" rel="stylesheet" />
<input type='text' id="date" class="" />
Also, air-timepicker docs: http://t1m0n.name/air-datepicker/docs/

Related

Trying to only allow date picking to be between 45-90 days from today's date

This is the code I have so far but I am not sure how to alter this function to also check if the selection was made after 90 days. What I have so far is when you click the date picker, it will jump to 45 days from today and not allow user to choose anything from prior, but the problem is that it will allow past 90 days which is what i dont want.
https://jsfiddle.net/dwyvtajg/
<label id="asterisk">*</label> <label class="description" for="element_2">Event Date:</label><br /><label>Reservations must be made between <b>45-90 days in advance</b> of the event date</label> <br /><input onclick="one()" required="" type="date" oninput="this.className = ''" name="Event-Date" id="datepicker1" min="2019-10-02" />
<script>if ( $('#datepicker1')[0].type != 'date' ) $('#datepicker1').datepicker();</script>
<script>
function formatISOLocal(d) {
let z = n => ('0' + n).slice(-2);
return d.getFullYear()+'-'+z(d.getMonth()+1) + '-' + z(d.getDate());
}
function one() {
let inp = document.querySelector('#datepicker1');
let d = new Date(new Date().getTime()+(45*24*60*60*1000));
inp.min = formatISOLocal(d);
inp.defaultValue = inp.min;
d.setFullYear(d.getFullYear() , d.getMonth() + 6);
inp.max = formatISOLocal(d);
// Debug
console.log(inp.outerHTML);
}
</script>
thanks for the help everyone.... editing this line fixed it!!!
d.setFullYear(d.getFullYear() , d.getMonth() , d.getDate() +45);
Definitely take like an hour to review the docs. You're making things so much harder on yourself otherwise.
For instance, setting a min and max date - voila:
$(function() {
$( "#datepicker1" ).datepicker({
maxDate:'+90d',
minDate: '+45d',
onSelect: function(date) {}
});
});

Bootstrap datetimepicker disable future date and time

I am trying to disable future hours within today using disabledTimeIntervals but it doesn't seem to work.
What I need to do is disable future hours in timepicker for today only, because disabling dates I can do with maxDate.
<input class="form-control input-sm pull-left " id="dp" placeholder="To" type="text">
$('#dp').datetimepicker({
maxDate: '0',
disabledTimeIntervals: [[moment(), moment().hour(24).minutes(0).seconds(0)]],
format: 'm/d/Y H:i A',
timepicker: true,
});
JSFIDDLE: https://jsfiddle.net/3oxmccjq/1/
You can use maxTime option and function onChangeDateTime to set the minTime according to the selected date.
The comparison between dates is up to you :-)
var today = new Date();
var options = {
maxDate: new Date(),
maxTime: new Date(),
disabledTimeIntervals: [
[moment(), moment().hour(24).minutes(0).seconds(0)]
],
format: 'm/d/Y H:i A',
timepicker: true,
onChangeDateTime: function(date) {
// Here you need to compare date! this is up to you :-)
if (date.getDate() === today.getDate()) {
this.setOptions({maxTime: new Date()});
} else {
this.setOptions({maxTime: false});
}
}
};
$('#dp').datetimepicker(options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/build/jquery.datetimepicker.full.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-datetimepicker/2.5.4/jquery.datetimepicker.css" rel="stylesheet" />
<input class="form-control input-sm pull-left " id="dp" placeholder="To" type="text">
The datetimepicker you are using in your jsfiddle have no options as disabledTimeIntervals and that's why it is not working. You should use disabledMinTime & disabledMaxTime
As for the solution I am guessing you want to disable all future hours for today but still want to allow future dates with all the possible time. Here is a jsfiddle for same :-
https://jsfiddle.net/sahilbatla/zpzL2po3/
Code looks like this :-
var today = new Date();
var todayEndOfDay = new Date().setHours(23,59,59,999);
$('#dp').datetimepicker({
disabledMinTime: today,
disabledMaxTime: todayEndOfDay,
format: 'm/d/Y H:i A',
timepicker: true,
onChangeDateTime: function(date) {
if (date.getDate() !== today.getDate()) {
this.setOptions({disabledMinTime: false, disabledMaxTime: false})
} else {
this.setOptions({disabledMinTime: today, disabledMaxTime: todayEndOfDay})
}
}
});

Display age on selecting date from date picker

I have written following jQuery for calculating age when user selects date from date picker and the focus of textbox(#dobfield) is lost then the calculated age should get displayed on textbox (#age).
function GetAge(dateString) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
$(document).ready(function() {
$("#dobfield").onblur(function() {
var dob = $("#dobfield").val().trim();
var calculatedDob = GetAge(dob);
$("#age").text(calculatedDob);
});
});
The html is as
<input aria-required="true" id="dobfield" class="form-control" required="" type="text" onclick="css()">
<input type="text" id="age" class="form-control" required>
The above code is not working!!!
Please help !!!
All the comments and answers here are great. However, the only problem with your code was that you were using the incorrect method to update the textbox.
To change the text of an input field (textbox):
$("#inputId").val("new value");
To change the text of a different element (label, span, et.c.):
$("#otherId").text("new value");
Here is the exact example what you want. This function will calculate age as soon as you change the date from datepicker and it will show the age in the field. In this example #dob is datepicker field's id.
$('#dob').datepicker({
onSelect: function(value, ui) {
var today = new Date(),
dob = new Date(value),
age = new Date(today - dob).getFullYear() - 1970;
$('#age').value(age); // Changed it to `val()` as it is the textbox.
},
maxDate: '+0d',
yearRange: '1920:2010',
changeMonth: true,
changeYear: true
});
jsfiddle
Check it out.
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/3/css/bootstrap.css" />
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<input type="text" name="birthdate" value="10/24/1984" />
<input type="text" id="age" class="form-control" required readonly>
<script type="text/javascript">
$(function() {
$('input[name="birthdate"]').daterangepicker({
singleDatePicker: true,
showDropdowns: true
},
function(start, end, label) {
var years = moment().diff(start, 'years');
alert("You are " + years + " years old.");
$("#age").val("You are " + years + " years old.");
});
});
</script>
Example: https://jsfiddle.net/m2fdprrL/
Same with appending the result to age field. https://jsfiddle.net/m2fdprrL/2/

Air Datepicker inline javascript for enabling date 15 days from newDate

I am using air datepicker for a form element and want to set the calendar to show dates which are 15 days from the current date and disabling selection of all previous dates.
However, I haven't been able to find a solution to this.
The following is the input field code so far:
<input type="text" data-date-format="dd-mm-yyyy" data-range="true" data-multiple-dates-separator=" - " data-language="en" data-startDate= newDate(+new Date + 12096e5) class="datepicker-here" name="Schedule">
Any help will be really appreciated.
Using jQuery UI
var dateToday = new Date();
var dates = $("#date").datepicker({
defaultDate: "+15",
changeMonth: true,
minDate: "+15",
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);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.16/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<label for="from">From</label> <input type="text" id="date" name="from"/>
Using air Datepicker
set minDate while initializing datepicker
var dateToday = new Date();
var dates = $("#date").datepicker({
language: 'en',
minDate: getMinDate(15)
});
function getMinDate(days) {
var date = new Date();
date.setDate(date.getDate() + days);
return date;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/css/datepicker.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/air-datepicker/2.2.3/js/i18n/datepicker.en.min.js"></script>
<label for="from">From</label> <input type="text" id="date" name="from"/>

I want to disable the past time in datetimepicker in codeigniter

i have 2 views. one of them calls the other one for the script of the datetime picker. right now what i get is a datetime picker that had past dates disabled. what i want to get is a date picker that not only has past date disabled but also has the passed time disabled. i am posting my code below.
This is my first view where the datepicker is displayed.
<input class="field-input form_datetime_houseboat" type="text" readonly placeholder="Starting Date" name="fromdate" id="fromdate">
my 2nd view has the section:
<script type="text/javascript">
var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
var dt = new Date();
var timed = new Date(dt.getHours(), dt.getMinutes());
$(".form_datetime").datetimepicker({format: 'dd-mm-yyyy hh:ii', startDate:today, minTime: timed});
</script>
thanks in advance.
try this script :helps to disable previoue date and time. create new date object as minDate
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.simple-dtpicker.js"></script>// datetime picker
<div id="date_picker"> </div>
<script type="text/javascript">
$(function(){
var dateToday = new Date();
var dates = $('#date_picker').dtpicker({
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);
}
});
});
</script>

Categories