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');
}
});
}
});
I'm using bootstrap date picker in my project. It's a session booking project. From the admin panel, I add the sessions for specific dates and I want the user's of my website to be able to see the dates for which I have added a session. My frontend receives the data from database. The data contains all the dates for which I have added a session. I want my datepicker to display only these dates from the data and disable the other dates.
Currently I have temporarily used a select box to solve this issue. But a datepicker would be better as it looks good is easy to navigate.
See the picture below. This is how I have used a select box to temporarily solve the problem
Here is the desired output that I want
It should be a datepicker with only those dates enabled which I receive from the database. The other dates should be disabled
I tried searching it on google but I'm not able to find the solution. Is this possible using bootstrap date picker? If yes, please suggest a workaround.
You can use beforeShowDay function to enable only the dates returned from your back end system.
Documentation here
This function is executed for every date, it checks if it is present in the list of applicable dates, returns true if present and enables it, else returns false and disables it.
$(function () {
let enabledDates = ['2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08'];
$('#datepicker').datepicker({
format: 'yyyy-mm-dd',
beforeShowDay: function (date) {
let fullDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
return enabledDates.indexOf(fullDate) != -1
}
});
});
beforeShowDay function also allows you to return classes for custom styling
beforeShowDay: function (date) {
let fullDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
if (enabledDates.indexOf(fullDate) != -1) {
return {
classes: 'enabled',
tooltip: 'You can select this date'
};
} else
return false
}
.enabled {
background: #DCDCDC;
}
None of the other solutions worked for me so here is my solution.
The documentation is not so clear and lacks of example but you can see a function that takes a date as a parameter and returns a Boolean, indicating whether or not this date is selectable
In this snippet, look at January 2020 for example in order to see only the active dates.
$(document).ready(function() {
var datesEnabled = [
'2021-01-01', '2021-01-11', '2021-01-21',
'2021-02-01', '2021-02-11', '2021-02-21',
'2021-03-01', '2021-03-11', '2021-03-21',
'2021-04-01', '2021-04-11', '2021-04-21',
'2021-05-01', '2021-05-11', '2021-05-21'
];
$("#datepicker-lorem").datepicker({
language: "fr",
autoclose: true,
todayHighlight: true,
todayBtn: true,
title: 'Test ;-)',
weekStart: 1,
format: 'dd/mm/yyyy',
// On n'active que les dates possibles
beforeShowDay: function(date) {
var allDates = date.getFullYear() + "-" + ('0' + (date.getMonth() + 1)).slice(-2) + "-" + ('0' + date.getDate()).slice(-2);
if (datesEnabled.indexOf(allDates) != -1) {
return {
classes: 'date-possible',
tooltip: 'Vous pouvez choisir cette date'
}
} else {
return false;
}
}
});
});
/* For better frontend result, add class to active date and add opacity to disabled date */
td.day.disabled {
opacity: 0.4;
}
td.date-possible {
background-color: red;
color: white;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/locales/bootstrap-datepicker.fr.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<input class="form-text form-control" type="text" id="datepicker-lorem" name="date_demande" value="" size="60" maxlength="128">
</div>
</div>
</div>
Using 'beforeShowDay' parameter you can disable dates:
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="//jqueryui.com/jquery-wp-content/themes/jqueryui.com/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() {
//date list that you want to disable
disableddates = ['10-10-2018', '10-11-2018', '10-12-2018'];
$("#datepicker").datepicker({
format: 'dd-mm-yyyy',
beforeShowDay: function(date) {
var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();
var currentdate = (m + 1) + '-' + d + '-' + y;
for (var i = 0; i < disableddates.length; i++) {
// Now check if the current date is in disabled dates array.
if ($.inArray(currentdate, disableddates) != -1) {
return [false];
}
}
return [true];
},
autoclose: 1,
todayHighlight: 1,
startView: 2,
minView: 2,
});
});
</script>
</head>
<body>
<p>Date:
<input type="text" id="datepicker">
</p>
</body>
</html>
</body>
</html>
Could someone help me to change code, I want to let my members choose a sunday between March – October (of every year ).
But only the 2nd or 4th Sunday of the month.
Thanks for help!
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<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 type="text/javascript">
$(function() {
$("#datepic").datepicker(
{ beforeShowDay: function(day) {
var day = day.getDay();
if (day == 2 || day == 5| day == 4| day == 6| day == 1| day == 3)
{
return [false, "somecssclass"]
} else {
return [true, "someothercssclass"]
}
}
});
});
</script>
</head>
<body>
<p>Uw voorkeursdatum: <input id="datepic"/>
</body>
</body>
</html>
beforeShowDay option is the right place to allow 2nd and 4th sundays only to be picked.
In order to do this, I used two "flags" (variable used to determine a certain state within a loop).
One flag which toggles each time a sunday is encountered in the loop.
And one to be sure to consider only the current month sundays.
DatePicker is looping through each table cells in the function provided in the beforeShowDay option.
And even if it isn't "rendered", days from the previous or next months ARE going through this loop.
I also used a 100ms timeout to reset these flags once a month has been drawn.
It is needed when navigating from a month to another.
Here is the function working in this CodePen.
I left a couple useful console.log.
;)
$(function() {
var even=false;
var inCurrentMonth=false;
$("#DatePicker").datepicker({
beforeShowDay: function(fulldate) {
//console.log(fulldate);
var day = fulldate.getDay();
var date = fulldate.getDate();
// In current month when the date 1 is found.
if(date==1){
inCurrentMonth=!inCurrentMonth;
// To reset values right after month draw.
setTimeout(function(){
even=false;
inCurrentMonth=false;
},100);
}
// This condition prevents conting syndays frome the previous month.
if(inCurrentMonth){
// If NOT a sunday.
if ( day != 0 ){
return [false,"Otherdays"]
} else {
// If this sunday is even (2nd or 4th)
if(even){
even=!even;
return [true,"Selectable-Sunday","You can select me!"]
}else{
even=!even;
return [false,"Unselectable-Sunday"]
}
}
}
}
}).focus();
});
I am hoping you can help.
I am trying to use a date format field in HTMl, which will then use this date in JavaScript. Once it has read the date, it will work out whether it is within 3 months or not.
At the moment, it is returning saying invalid date entered. How do I get JavaScript to read the date when using a HTML date format?
<!DOCTYPE html>
<html>
<head>
<title>ICT5</title>
<script>
function checkdate() {
var UserDate = document.getElementById("date").value;
var parts = UserDate.split("/");
var dt = new Date(parseInt(parts[2], 10),
parseInt(parts[1], 10),
parseInt(parts[0], 10));
window.alert("userdate is" +dt);
var timestamp = new Date().getTime() + (90 * 24 * 60 * 60 * 1000);
window.alert("" +timestamp);
if (timestamp > dt){
alert("Selected time is less than 3 months from now");
} else {
alert("Selected time is more than 3 months from now");
}
}
</script>
</head>
<body>
<h1>ICT 5 a</h1>
<h1> Working out the difference between today and your selected date </h1>
<p> Please enter a date </p>
<input id="date" type="date">
<button type="button" onclick="checkdate()">Submit</button>
</body>
</html>
I use flip clock JS for count up but when i reload the page time start from zero
how could i save time like this
var clock;
$(document).ready(function() {
clock = $('.clock').FlipClock({
clockFace: 'DailyCounter'
});
});
Here's one possible solution:
Consider that it will not reset days value at the end of the month.
But if you want you can set it to "autoStart: false" and "setInterval"...
$(document).ready(function() {
var date = new Date(),
days = date.getDate()*60*60*24,
hours = date.getHours()*60*60,
minutes = date.getMinutes()*60,
sec = date.getSeconds(),
clock = $('.clock').FlipClock({
clockFace: 'DailyCounter'
});
clock.setTime(days+hours+minutes+sec);
});
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="http://flipclockjs.com/_themes/flipclockjs/css/flipclock.css">
</head>
<body>
<div class="clock"></div>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://flipclockjs.com/_themes/flipclockjs/js/flipclock/flipclock.js"></script>
</body>
</html>
playground