Disable some future dates in DateRangePicker - javascript

I'm trying to disable today date and 6 more days dates from today.
I know I can do that with maxDate but when I do, it disable all future dates.
My code:
$('input[name="datefilter"]').daterangepicker({
autoUpdateInput: false,
maxDate: new Date(),
"autoApply": true,
locale: {
"firstDay": 1,
"separator": " - "
}
});
I tried with maxDate: new Date(6) too but same error.
Any idea?

You can use the isInvalidDate function to return true/false if the date is valid or not:
invalid_dates = ['2017-01-11', '2017-01-12', '2017-01-13']
$(function() {
$('input[name="daterange"]').daterangepicker({
isInvalidDate: function(date)
{
return !!(invalid_dates.indexOf(date.format('YYYY-MM-DD')) > -1);
}
});
});
<!-- 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="daterange" value="01/01/2017 - 01/10/2017" />

That's not an error, that's the expected behavior of the property you're using. You're telling it "don't allow any date beyond this". According to the docs, there is an option for isInvalidDate that Dekel found in his answer.
I was about to delete this answer, but decided against it due to pointing out that it wasn't an error, but the behavior of the property that was being used. Anyone should definitely upvote the other answer for finding that property first.

Related

Is there any method to click on a flatpickr date automatically using jQuery?

I tried to auto-click on below calendar created with flatpickr using jQuery but it's not working, Any method to do that?
Image: Screenshot of Calander.jpg
<span class="flatpickr-day click-this" aria-label="October 9, 2022" tabindex="-1">9</span>
jQuery (function($) {
$(document).ready(function() {
$(".click-this").trigger("click")
})
})
Can someone help me to click automatically on the date on calendar with click-this class?
Assuming from the context of your question that you're trying to set the default date displayed in the flatpickr control, then you don't need to auto-click anything. You can simply set the defaultDate property in the settings object you provide when instantiating the library:
const today = new Date();
const tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);
$("#date").flatpickr({
inline: true,
defaultDate: tomorrow
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.js" integrity="sha512-K/oyQtMXpxI4+K0W7H25UopjM8pzq0yrVdFdG21Fh5dBe91I40pDd9A4lzNlHPHBIP2cwZuoxaUSX0GJSObvGA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.13/flatpickr.min.css" integrity="sha512-MQXduO8IQnJVq1qmySpN87QQkiR1bZHtorbJBD0tzy7/0U9+YIC93QWHeGTEoojMVHWWNkoCp8V6OzVSYrX0oQ==" crossorigin="anonymous" referrerpolicy="no-referrer"
/>
<input type="text" id="date" />
More details on the settings available when using flatpickr can be found in the documentation: https://flatpickr.js.org/options/

make the default single date picker date

I have a case where I use the daterangepicker library to retrieve the date, here is the jsfiddle link: https://jsfiddle.net/heriira_/0bgqxdLa/5/
$(function () {
var start = moment().subtract(1, "days");
$("#demo").daterangepicker(
{
singleDatePicker: true,
startDate: start,
},
function (start, end, label) {
console.log(start.format("YYYY-MM-DD"));
}
);
});
in the code above how to make a handle if, for example, the default value is yesterday, and when we choose a time that is more than yesterday, for example two days before it will return to the default, which is yesterday's date.
based on the documentation https://www.daterangepicker.com/#options
you just need to add minDate:
so the date before yesterday can't be selected
$(function() {
var start = moment().subtract(1, "days");
$("#demo").daterangepicker({
singleDatePicker: true,
startDate: start,
minDate: start
},
function(start, end, label) {
console.log(start.format("YYYY-MM-DD"));
}
);
});
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<input type="text" id="demo" value="10/24/1984" />

When i include datepicker js & css files through php include doesn't work, but works on if i put all the code in single php page

I am creating a PHP form so I am including PHP header files to my index page. all the files are correctly loading except jquery date picker files are not loading and if put all the jquery CDN files to the index page it works correctly. But I can't add all the codes in the index page. so help me out
<?php
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
<script src="js/custom.js"></script>
?>
<?php include 'inc/header.php';?>
<div class="ui calendar" id="example1">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text" placeholder="Date/Time">
</div>
</div>
//
$('#example1').calendar();
$('#example2').calendar({
type: 'date'
});
$('#example3').calendar({
type: 'time'
});
$('#rangestart').calendar({
type: 'date',
endCalendar: $('#rangeend')
});
$('#rangeend').calendar({
type: 'date',
startCalendar: $('#rangestart')
});
$('#example4').calendar({
startMode: 'year'
});
$('#example5').calendar();
$('#example6').calendar({
ampm: false,
type: 'time'
});
$('#example7').calendar({
type: 'month'
});
$('#example8').calendar({
type: 'year'
});
$('#example9').calendar();
$('#example10').calendar({
on: 'hover'
});
var today = new Date();
$('#example11').calendar({
minDate: new Date(today.getFullYear(), today.getMonth(), today.getDate() - 5),
maxDate: new Date(today.getFullYear(), today.getMonth(), today.getDate() + 5)
});
$('#example12').calendar({
monthFirst: false
});
$('#example13').calendar({
monthFirst: false,
formatter: {
date: function (date, settings) {
if (!date) return '';
var day = date.getDate();
var month = date.getMonth() + 1;
var year = date.getFullYear();
return day + '/' + month + '/' + year;
}
}
});
$('#example14').calendar({
inline: true
});
$('#example15').calendar();
I want to include datepicker js & css files through php file using instead of putting all cdn files to index.php
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="https://cdn.rawgit.com/mdehoog/Semantic-UI/6e6d051d47b598ebab05857545f242caf2b4b48c/dist/semantic.min.js"></script>
<script src="js/custom.js"></script>
Remove php tags and your scripts will be included accurately. You are using html tags inside php block.That's the reason they are not rendered.

Amsul DateTimePicker - How to unselect active date?

I am using amsul DateTime picker (https://amsul.ca/pickadate.js/date/) where if you see in the preview the today's date is highlighted in green. Now if you move the calendar to the next month or previous month. Every month's today's date is also highlighted.
How do I unhighlight any other dates then today's date?
$('.datepicker').pickadate()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://hateable-tests.000webhostapp.com/classic.css" rel="stylesheet">
<link href="https://hateable-tests.000webhostapp.com/classic.date.css" rel="stylesheet">
<link href="https://hateable-tests.000webhostapp.com/classic.time.css" rel="stylesheet">
<script src="https://hateable-tests.000webhostapp.com/picker.js"></script>
<script src="https://hateable-tests.000webhostapp.com/legacy.js"></script>
<script src="https://hateable-tests.000webhostapp.com/picker.date.js"></script>
<script src="https://hateable-tests.000webhostapp.com/picker.time.js"></script>
<input type="text" class="datepicker">
I belive this is the answer:
https://github.com/amsul/pickadate.js/issues/805
ilnee commented on 2 Oct 2016
I ran into this issue as well. As a workaround, I remove the
picker__day--highlighted class from every div with that class that
doesn't have today's timestamp as data-pick value:
var now = new Date();
var timestamp = new Date(now.getFullYear(), now.getMonth(), now.getDate()) / 1;
$('.datepicker').pickadate({
onRender: function() {
$('div.picker__day--highlighted').each(function(index, value) {
if ($(this).data('pick') !== timestamp) {
$(this).removeClass('picker__day--highlighted');
}
});
}
});

How do I get Date Range Picker selected date to a variable?

hi i am using data range picker for filter option. there is change with default date picker range is . here i am using a div instead of text box. so tthat i need the selected start date and end date in a variable how can i due it?. i try like this way...
$('#Date').daterangepicker();
$(document).on("click",".applyBtn",function() {
// var range = $('#Date').datarangepicker.getRange();
// var startDate = range.start;
// var endDate = range.end;
var x =$('#Date').data('daterangepicker').StartDate()
alert(x);
});
<!-- 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" />
<div id="Date" class="col-xs-5 col-md-4 paddingNull filterImageAlign" >here select</div>
Try this.
var startDate = $('#Date').data('daterangepicker').startDate._d;
var endDate = $('#Date').data('daterangepicker').endDate._d;
If you need to get it formatted locally
$('#Date').daterangepicker().on('apply.daterangepicker', function (e, picker) {
var startDate = picker.startDate.format('DD-MM-YYYY');
var endDate = picker.endDate.format('HH:mm');
})

Categories