Highlight certain dates on bootstrap-datepicker - javascript

I'm using the bootstrap-datepicker to check the days where my website got sales and I want to display or highlight the dates there was at least one sale. I can pass the dates on JSON and then make them into a Javascript's array, but I don't know how to make the datepicker to fetch the dates and highlight them.
Is there a way with the bootstrap-datepicker to achieve this?

here is another example to highlight a range of dates:
var inRange=false;
$('#elementPicker').datepicker({
beforeShowDay: function(date) {
dateFormat = date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
if (dateFormat == '2014-01-01') {
inRange = true; //to highlight a range of dates
return {classes: 'highlight', tooltip: 'Title'};
}
if (dateFormat == '2014-01-05') {
if (inRange) inRange = false; //to stop the highlight of dates ranges
return {classes: 'highlight', tooltip: 'Title'};
}
if (inRange) {
return {classes: 'highlight-range'}; //create a custom class in css with back color you want
}
}
});

$('#xxx').datepicker()
.on('onRender', function(ev){
if (ev.date.valueOf() == your date){
return 'highlight';
}
});
Might do the trick, although I am not sure.

There is simple way to set multiple dates in bootstrap calendar.
There is a property multidate which can be used for the selection.
find the working code below.
$('.inline_date').datepicker({
multidate: true,
todayHighlight: true,
minDate: 0,
});
$('.inline_date').datepicker('setDates', [new Date(2015, 7, 5), new Date(2015, 7, 8), new Date(2015, 7, 7)])
Only one problem there the highlights are removed on click. and it take month as one less. if you want August dates then you have to use 7 not 8.

My solution of this problem using Knockout bindings and datepicker property "beforeShowDay":
function MainFilters() {
var self = this;
self.notAppliedDates = ko.observableArray([]);
self.customDates = ko.observableArray(["14.06.2015", "20.06.2015", "26.06.2015", "10.06.2015", "29.06.2015"]);
}
ko.bindingHandlers.multiDatepicker = {
init: function (element, valueAccessor, allBindings) {
var customDates = allBindings.get("customDates");
valueAccessor()();
$(element).datepicker(
{
multidate: true,
language: "ru",
orientation: "top",
beforeShowDay: function (date) {
var d = ConvertDateTostring(date, "dd.mm.yyyy");
if ($.inArray(d, customDates()) != -1) {
return {
classes: 'activeClass'
};
}
return;
}
}
)
.bind(
'changeDate',
function (e) {
var res = e.dates;
var value = [];
res.sort(function (a, b) { return a - b });
for (var i in res) {
value.push(res[i].asString());
}
valueAccessor()(value);
}
);
},
update: function (element, valueAccessor, allBindings, bindingContext) {
}
};
function ConvertDateTostring(date, format) {
if (!date) {
return "";
}
if (format) {
return dateFormat(date, format);
}
return dateFormat(date, "dd.mm.yy");
}
.activeClass{
background: #32cd32;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input type="text" id="dates" data-bind="customDates: customDates, multiDatepicker: notAppliedDates, value: selectedDatesString">

Using datetimepicker events show, update and change the date need to be highlighted.
The Blog post here explaining how it can achieve using Bootstrap Datetimepicker.
$('#datetimepicker').on('dp.show', function(e){
highlight()
})
$('#datetimepicker').on('dp.update', function(e){
highlight()
})
$('#datetimepicker').on('dp.change', function(e){
highlight()
})
function highlight(){
var dateToHilight = ["04/19/2019","04/20/2019","04/30/2019"];
var array = $("#datetimepicker").find(".day").toArray();
for(var i=0;i -1) {
array[i].style.color="#090";
array[i].style.fontWeight = "bold";
}
}
}
For more information see the blog
Reference:
https://sourcecodezoneseven.blogspot.com/2019/04/here-is-code-function-hilight-var.html

I tried Matias' answer above and had a flew glitches. After a bit of trouble shooting and slightly modifying Matias' answer, I managed to create a working solution.
If you paste the below code into a blank .html file then you should see a input box with a date widget attached to it.
The start date (8th August 2021), end date (26th August 2021) and dates in between should have their own distinct colour scheme.
<script
src="https://code.jquery.com/jquery-3.6.0.js"
integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js" integrity="sha256-VBLiveTKyUZMEzJd6z2mhfxIqz3ZATCuVMawPZGzIfA=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/js/bootstrap-datetimepicker.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.42/css/bootstrap-datetimepicker.min.css" rel="stylesheet"></link>
<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>
<style>
.StartDate{background-color: green;}
.InBetweenDates{background-color: darkred;}
.EndDate{background-color: blue;}
</style>
<p>Date: <input type="text" readonly="" id="elementPicker"></p>
<script>
var inRange=false;
$('#elementPicker').datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true,
minDate: new Date("01/01/2016"),
beforeShowDay: function(date){
console.log("date")
console.log(date)
dateFormat = date.getUTCFullYear() + '-' + date.getUTCMonth() + '-' + date.getUTCDate();
console.log("dateFormat")
console.log(dateFormat)
// 7 means August (0 means January)
// We are checking to see if the date is 8th August 2021
// Matias had if (dateFormat == '2014-01-01') but after looking at Console.Log,
// I removed the leading 0 from the day and month figure.
if (dateFormat == '2021-7-8') {
console.log("(dateFormat == '2021-7-8')")
inRange = true; //to highlight a range of dates
// If the day is 8th August 2021 (remember 7 means August not July)
// then we add the CSS class StartDate to the day (look at the top of the code for the StartDate CSS class styling).
return [true, 'StartDate', 'tooltipText'];
}
if (dateFormat == '2021-7-26') {
console.log("(dateFormat == '2021-7-26')")
inRange = false; //to stop the highlight of dates ranges
// If the day is 26th August 2021 (remember 7 means August not July)
// then we add the CSS class EndDate to the day (look at the top of the code for EndDate CSS class styling).
return [true, 'EndDate', 'tooltipText'];
}
if (inRange) {
// If the day is between 8th August 2021 - 26th August 2021 then we add the CSS class InBetweenDates to the day (look at the top of the code for the StartDate CSS class styling).
return [true, 'InBetweenDates', 'tooltipText'];
} else {
// I think this essentially means select the day, do nothing with it and then move on.
// Adding this in prevented the error message 'Uncaught TypeError: Cannot read property '0' of undefined jquery datepicker'.
return [true];
}
}
});
</script>

beforeShowDay: function (date) {
dateFormat = (date.getUTCDate()+1).toString().padStart(2, '0') + '-' + (date.getUTCMonth()+1).toString().padStart(2, '0') + '-' + date.getUTCFullYear(); //this will format the dates that appears when you toggle the datpicker to the fromat you want ok..
console.log(dateFormat);
if ($.inArray(dateFormat, unavailableDates) == -1) { //certain days if they don't exist in my array of dates i already created depending on my project
return {classes:'avail_days'}; //this will produce a class called full_days with the date it finds it inside the array (unavailableDates) and you have to style with css THAT avail_days class
}else {
return [true];
}
},
//gratitude appreciated ;)

Related

Updating Full Calendar Day At Midnight

//Reset At Midnight
function resetAtMidnight() {
var now = new Date();
var night = new Date(
now.getFullYear(),
now.getMonth(),
now.getDate() + 1, // the next day, ...
0, 0, 0 // ...at 00:00:00 hours
);
var msToMidnight = night.getTime() - now.getTime();
setTimeout(function() {
reset(); // <-- This is the function being called at midnight.
resetAtMidnight(); // Then, reset again next midnight.
}, msToMidnight);
}
function reset() {
$('#calendar').fullCalendar('today');
}
I am trying to have FullCalendar.js update the current day everyday at midnight. I pulled this reset snippet from another post on here - but the reset function does not seem to execute.
You have not provided full detail so I don't know what exactly you need but following code might help you to solve your issue.
You can check midnight quite easily using moment.js library.
Following code was taken from another post Best Way to detect midnight and reset data
$(document).ready(function() {
var midnight = "0:00:00";
var now = null;
setInterval(function() {
now = moment().format("H:mm:ss");
if (now === midnight) {
alert('reset() function here');
}
$("#time").text(now);
}, 1000);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<code id="time"></code>
Fullcontrol:
You can either use rerenderEvents or refetchEvents as per your requirement. So it will be something like this
function reset(){
$('#calendar').fullCalendar('rerenderEvents');
OR
$('#calendar').fullCalendar('refetchEvents');
}
If you want to re-draw the fullcontrol then here is another informative post 're-draw fullcalendar'
As post suggested you can do
$('#calendar').fullCalendar('destroy');
$('#calendar').fullCalendar('render');

Datetime Picker schedule time to 8 hrs from current time

I'm using DateTimePicker jQuery plugin by XDSoft, please check below image
My requirement is that, if i selected current date,(9th Nov, 2015) then the time from time picker should show 6hrs add to current time...
I mean, in above image current Date is selected... and current time is 12.00 but i want that 12.00 & above time should be disable..
How can we do that..??
Based on XDSoft DateTimePicker documentation, here's what can be done:
var logic = function( currentDateTime ){
var d1 = new Date();
// Check that it's today, so we need to restrict time chooser
if (currentDateTime.getDate() == d1.getDate() && currentDateTime.getMonth() == d1.getMonth())
{
// Adding six hours
d1.setHours ( d1.getHours() + 6 );
// Creating 'HH:MM' string
var defaultTime = (d1.getHours() < 10 ? "0" : "") + d1.getHours() + ":" + (d1.getMinutes() < 10 ? "0" : "") + d1.getMinutes();
// Enforce time restriction
// ('this' is jquery datetimepicker object)
this.setOptions({
minTime : defaultTime,
defaultTime : defaultTime
});
}
else
{
// Lift time restriction if selected day is not today
this.setOptions({
minTime : false,
defaultTime : false
});
}
};
// Initiate datepicker with custom logic
$('#datetimepicker').datetimepicker({
onChangeDateTime:logic,
onShow:logic
});
Adding 6 hours solution based on: https://stackoverflow.com/a/13034220/2715393

FULLCALENDAR: How to disable prev button and only be able to see current month and the next?

I downloaded: http://arshaw.com/fullcalendar/
Does somebody know how i can disable the prev button, so you can only see the current month and how i can limit the next button, so you can only see one next month?
I put this in the head:
$(document).ready(function() {
var date = new Date();
var d = date.setDate();
var m = date.setMonth();
var y = date.setFullYear();
$('#calendar').fullCalendar({
editable: true,
events: [
{
You can disable the prev/next button by just adding 'fc-state-disabled' class.
e.g.
//disballing prev button
$(".fc-button-prev").addClass('fc-state-disabled');
Also if you just want to allow to show only next month on clicking on next button then add following code.
function checkNext(e) {
if (isNextallowed == true) {
isNextallowed = false;
return true;
} else {
$(".fc-button-next").addClass('fc-state-disabled');
e.preventDefault();
return false;
}
}
$(".fc-button-next").on("click", checkNext);
$(".fc-button-next .fc-text-arrow").on("click", checkNext)
Checkout this Demo Fiddle
try -
$('#calendar').fullCalendar({
editable: true,
header : {
left : 'next today'
}
});
You can also refer to the documentation of the fullCalendar API to get more insight into how it works and other possible options. Refer - Full Calendar from Adam Arshaw
Here's my simple solution.
Place this code in the renderView function (around line 368 in version 1.5.4)) before ignoreWindowResize--; near the end of the function.
var lammCurrentDate = new Date();
var lammMinDate = new Date( lammCurrentDate.getFullYear(), lammCurrentDate.getMonth(), 1, 0, 0, 0, 0);
if (currentView.start <= lammMinDate){
header.disableButton('prev');
} else {
header.enableButton('prev');
}

jquery ui datepicker go to today button

i need my calendar to go to the current date when [today] button is clicked, so that the user can get to the current day when browsing my calendar.
for example the current month is feb 2013
and the user is browsing on the dec 2015 page of the calendar when he clicks the [today] button it will automatically return to the current days page of the calendar which is
feb 2013.
i'am using
jquery-ui-datepicker.min.js
as a plugin for my calendar
The jQuery UI Datepicker has functionality built into it for this.
See this: http://jqueryui.com/datepicker/#buttonbar
$(function() {
$( "#datepicker" ).datepicker({
showButtonPanel: true
});
});
You could do:
function TodaysDate() {
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
return month + "/" + day + "/" + year;
}
$("#btnToday").click(function() {
var today = new Date();
$(target).datepicker('setDate', TodaysDate());
}
Where target is your date picker control ID.

fullcalendar, how to limit the number of events per day in the month view

I have many events on a day, and it works as expected but now looking at the month view, my calendar grid is much taller that expected. I'd like to hide some of these events from the month view, like a summary with a visual que that there are more on this day than can be shown.
I can use eventRender and return false, but i would like to know how many events are on a given day, so i can limit the rendering to about 4, then perhaps i would add an event that says " more ... "
So the question may be : how to count the events on a given date ?
or is this more like a feature request to expose a max counter for month view ?
thanks
As of FullCalendar 2.1.0, this feature is included, just use:
$('#calendarBlock').fullCalendar({
eventLimit: true
[...]
});
Demo: http://arshaw.com/js/fullcalendar-2.1.0-beta2/demos/agenda-views.html
Documentation: http://fullcalendar.io/docs/display/eventLimit/
I have a similar requirement and was thinking of doing something like this:
in json feed, I would return special events that contain a count of events for each day.
Using appropriate full calendar eventRender callback, I would only display these special total count events depending on view by returning false in eventRender() for any events I don't want to see.
I haven't implemented this yet, but perhaps it can be a valid avenue. Correct me if I'm wrong.
This buggy (mostly abandoned) plugin does exactly what you're asking for. Check it out, and feel free to contribute. Warning: It is a hack, but fullcalendar itself has an API that needs a lot of work. Most of the important parts are trapped in a closure, which makes it very difficult to extend. There really is no way to accomplish this without hackery, unless you want to edit the plugin's source code.
That being said, this is the closest I have found to what you are asking for: https://github.com/lyconic/fullcalendar/tree/view-more
currently not possible. see feature request:
http://code.google.com/p/fullcalendar/issues/detail?id=304
You don't need to take much effort on taking the total count of events.
I have got a simple way to count total events on my fullCalendar. In the eventAfterAllRender , write something like this,
eventAfterAllRender: function(view) {
var allevents = $('#calendar').fullCalendar('clientEvents');
var countevents = 0;
if( allevents.length ) {
countevents = countevents + allevents.length;
}
if(!countevents) {
// alert('event count is'+countevents);
console.log('event count is',countevents);
}
}
-where calendar is my calendar #
This is all my way of doing. I am taking left from here. But I am sure, it is not that harder if you try something on the loading: function() of the fullCalendar to limit the number of events according to your wish.
Thanks though.
I did something like this:
function(start, end, callback) {
var viewName = $("#calendar").fullCalendar('getView');
var startTime = Math.round(start.getTime() / 1000);
var endTime = Math.round(end.getTime() / 1000);
MyWebService.MyWebMethod.GetEventsByView(startTime, endTime, viewName.name,
function(args) {
callback(args);
},
function(error) {
var e = error;
}
);
}
Here the GetEventsByView method returns a grouped set for month view and detailed set for agendaWeek and day views. But for this to work correctly, I had to set lazyFetching:false for the calendar.
I am doing like this
Css:
.MaxHght
{
height:16px;
}
.viewMore
{
float:right;
color: rgb(219, 104, 28);
cursor:pointer;
}
plugin code
dayClick: function (date, allDay, jsEvent, view) {
$('#fullCalendar').fullCalendar('changeView', 'agendaDay').fullCalendar('gotoDate', date.getFullYear(), date.getMonth(), date.getDate());
},
eventRender: function (event, element, view) {
element.qtip({
content: event.QText,
position: {
my: 'CenterBottom',
at:'TopCenter'
},
style: {
tip: 'bottomMiddle'
}
});
$(element).removeClass('MaxHght');
if (view.name == 'month') {
$(element).addClass('MaxHght');
var year = event.start.getFullYear(), month = event.start.getMonth() + 1, date = event.start.getDate();
var result = year + '-' + (month < 10 ? '0' + month : month) + '-' + (date < 10 ? '0' + date : date);
$(element).addClass(result);
var ele = $('td[data-date="' + result + '"]'),count=$('.' + result).length;
$(ele).find('.viewMore').remove();
if ( count> 3) {
$('.' + result + ':gt(2)').remove();
$(ele).find('.fc-day-number').after('<a class="viewMore"> More</a>');
}
}
},
eventAfterAllRender: function (view) {
if (view.name == 'month') {
$('td.fc-day').each(function () {
var EventCount = $('.' + $(this).attr('data-date')).length;
if (EventCount == 0)
$(this).find('.viewMore').remove();
});
}
},

Categories