Javascript multiple var arrays - javascript

I have the following bit of javascript that blocks off dates in a calendar and displays a message instead:
var unavailableDates = ["28-7-2017", "29-7-2017", "30-7-2017", "31-7-2017", "28-8-2017", "24-12-2017", "25-12-2017", "26-12-2017", "1-1-2018", "30-3-2018", "31-3-2018", "1-4-2018", "1-2-2018", "7-5-2018", "28-5-2018", "6-8-2018", "27-8-2018", "24-12-2018", "25-12-2018", "26-12-2018", "1-1-2019"];
jQuery(function($) {
$("#Booking1arrivaldate").datepicker({
minDate: 5,
beforeShowDay: function(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
return [true, ""];
} else {
return [false, "", "Closed to the public for drop off and collection."];
}
}
});
});
If someone tries to select one of the dates within the var unavailableDates then it won't let them and instead displays the message.
However I need to add another var with a completely different message if those dates are selected. I have tried a few combinations of the if statement but keep getting errors in my code.
Any ideas?

Related

HTML, open link click anywhere on page

I have code for clickable background that I want to use but when I click on background its always open. How to make this code to work one per day BUT at most easiest way possible... with cookie or something else. I really need help with this. Thanks!
<body onclick="location.href='test.html';">
You can use localStorage.
<script>
function onBodyClick() {
var lastOpened = localStorage.getItem('body-opened'); // You can use another identifier instead of 'body-opened'
if (lastOpened && new Date(lastOpened).toDateString() === new Date().toDateString()) {
return true;
} else {
localStorage.setItem('body-opened', new Date().toDateString());
document.location.href = 'test.htm';
}
}
</script>
<body onclick="onBodyClick()"></body>
If you want to restrict the user to open the link only once per day. You can do something like this:
<body onclick="openLink()">
<script>
function openLink() {
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
// As date object returns time as well. Which we dont need. So we remove that.
if(localStorage.getItem('date') == today) {
alert('come back tomorrow');
} else {
localStorage.setItem('date', today);
location.href='test.html';
}
}
</script>

jQuery datepicker: highlight and style multiple dates

I'm using the jQuery date picker as an online calendar too. I have a setup which takes the date values in the data-value attribute of the .whats-on-block divs, and assigns the .special class to the relevant date value in the jQuery date picker, thus I can style it accordingly via CSS.
This works great, I also have a .highlights div with multiple date values as the data-highlight attribute, I'm wondering if I can assign the .highlighter class to the relevant date values in the date picker so I can style these separately? I'm not really sure how to achieve this as the beforeShowDay function is already being used to assign the .special. Here's the markup...HTML
<div class="whats-on-grid">
<div class="datepicker-block" class="whats-on-block">
<div id="datepicker"></div>
</div>
<div class="whats-on-block" data-value="1982014"></div>
<div class="whats-on-block" data-value="2282014"></div>
<div class="whats-on-block" data-value="1482014"></div>
<div class="whats-on-block" data-value="2982014"></div>
//etc
</div>
<div class="highlights" data-highlight="2882014 2782014 1582014 2082014 1992014"></div>
CSS
.special { background-color: red !important; }
.highlighter { background-color: blue !important; }
jQuery
var $container = $('.whats-on-grid');
var $blocks = $("div.whats-on-block", ".whats-on-grid");
$(function () {
var blocks = $('.whats-on-grid .whats-on-block')
$('#datepicker').datepicker({
inline: true,
//nextText: '→',
//prevText: '←',
showOtherMonths: true,
//dateFormat: 'dd MM yy',
dayNamesMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
//showOn: "button",
//buttonImage: "img/calendar-blue.png",
//buttonImageOnly: true,
onSelect: function (dateText, inst) {
var date = new Date(dateText);
var dateValue = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();
$container.isotope({
filter: '[data-value~="' + dateValue + '"], #datepicker-block'
});
},
beforeShowDay: function (date) {
var target = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();
var contains = blocks.filter('[data-value~="' + target + '"]').length > 0;
return [true, contains ? 'special' : '', '']
}
});
});
Any suggestions on how this can be achieved would be greatly appreciated!
If all you are trying to do is to add the highlighter class to the highlights dates prior to rendering, you should be able to easily achieved that in your beforeShowDay function like this:
beforeShowDay: function (date) {
var target = date.getDate().toString() + (date.getMonth() + 1).toString() + date.getFullYear().toString();
var isSpecial = blocks.filter('[data-value~="' + target + '"]').length > 0;
var isHighlight = $("div[class~='highlights']").filter('[data-highlight~="' + target + '"]').length > 0;
if (isSpecial)
return [true, 'special', ''];
else if (isHighlight)
return [true, 'highlighter', ''];
return [true, '', ''];
}
The tricky part is what if a special date is also a highlight date. You may need different CSS styling to distinguish them.
Live demo available on fiddle.

Disabling dates and specific weekdays in jquery datepicker

I'm trying to disable specific dates (i.e christmas etc) PLUS disabling certain weekdays per default in the jQuery UI datepicker, but I can't get it to work. I have the following:
<script type="text/javascript">
iDays = 2;
blockDays = [1,4,6];
$(document).ready(function () {
$.datepicker.setDefaults($.datepicker.regional['sv']);
$('.inpDate').datepicker({
dateFormat: 'yy-mm-dd',
minDate: iDays,
maxDate: 14,
showOtherMonths: true,
showStatus: true,
beforeShowDay: noHolidays
});
var disabledDays = ["12-24-2013", "12-25-2013", "12-26-2013", "12-31-2013", "1-1-2014"]
function noHolidays(date) {
return [!disableSpecificWeekDays(date) && !nationalDays(date)];
}
function nationalDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
if ($.inArray((m + 1) + '-' + d + '-' + y, disabledDays) != -1 || new Date() > date) {
return true;
}
}
return false;
}
function disableSpecificWeekDays(date) {
var daysToDisable = blockDays;
var day = date.getDay();
for (i = 0; i < daysToDisable.length; i++) {
if ($.inArray(day, daysToDisable) != -1) {
return [false];
}
}
return [true];
}
});
</script>
If I run ONLY the disableSpecificWeekDays in the "beforeShowDay" parameter it works fine, same goes with the nationalDays. But for some reason it FEELS like it's simply ignoring the date parameter if I call it through the noHoliday function.
In short, I need help!
Just noticed your question after having answered a similar/duplicate question. Instead of copying the code from there just have a look here: Hide weekdays and specific dates of jquery datepicker

jquery datepicker datepicker beforeShowDay not working in next year

i am trying to enable dates in jQuery datepicker calendar, it works well for 2013 dates but 2014 dates it is not working.
here is my code
<script>
$(document).ready(function () {
var enabledDays = ['11-30-2013', '12-14-2013', '12-21-2013', '01-11-2014', '01-11-2014', '01-25-2014', '02-08-2014', '02-22-2014', ]
function enableAllTheseDays(date) {
var m = date.getMonth(),
d = date.getDate(),
y = date.getFullYear();
for (i = 0; i < enabledDays.length; i++) {
if ($.inArray((m + 1) + '-' + d + '-' + y, enabledDays) != -1) {
return [true];
}
}
return [false];
}
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yyyy',
beforeShowDay: enableAllTheseDays
});
});
</script>
how to get this to work in 2014 dates aswell.
Thanks
Might be too late to help, but drop the leading zero on months <=9. 03-10-2014 fails whereas 3-10-2014 works fine with the code you have. See the fiddle
$(document).ready(function () {
var enabledDays = ['11-30-2013', '12-14-2013', '12-21-2013', '3-10-2014', '11-30-2014']
function enableAllTheseDays(date) {
var m = date.getMonth(),
d = date.getDate(),
y = date.getFullYear();
for (i = 0; i < enabledDays.length; i++) {
if ($.inArray((m + 1) + '-' + d + '-' + y, enabledDays) != -1) {
return [true];
}
}
return [false];
}
$('#datepicker').datepicker({
dateFormat: 'mm-dd-yyyy',
beforeShowDay: enableAllTheseDays
});
});

Jquery DatePicker change highlighted color onSelect of a particular date

I have this code of Jquery datepicker that I am working on.
What I want to do is change the color of the current highlighted date to grey when I click on anyother date, and the color of the clicked date to green.
The tricky part is that only the ones which are grey can turn green and viceversa but no other dates can turn into green on click.
I dont understand what I am doing wrong. Or is it that my code is completely wrong.
If anyone can help.
here's the code
$(function() {
var togo=['10/25/2013']
var datesArray=['10/27/2013','10/28/2013']
var datesArray1=['10/25/2013','10/26/2013']
var datesArray2=['10/24/2013']
$( "#datepicker" ).datepicker({
numberOfMonths: 2,
selectMultiple:true,
beforeShowDay: function (date) {
var theday = (date.getMonth()+1) +'/'+
date.getDate()+ '/' +
date.getFullYear();
return [true,$.inArray(theday, datesArray2) >=0?"specialDate":($.inArray(theday, datesArray)>=0?"specialDate2":($.inArray(theday, datesArray1)>=0?"specialDate1":''))];
},
onSelect: function(date){
console.log("clicked"+date);
return [true,$.inArray(date, togo) >=0?"specialDate":($.inArray(date, datesArray1)>=0?"specialDate1":'')] ;
}
});
//$.inArray(theday, datesArray) >=0?"specialDate":'specialDate1'
});
For a clearer picture of what I am doing and what I want heres a JSFiddle
http://jsfiddle.net/pratik24/Kyt2w/3/
thanks.
onSelect does not behave like beforeShowDay. You cannot return an array with [true/false, class, popup]. Instead, you will have to apply the class manually in the function.
I'm not sure exactly what you are trying to do, but I would rearrange your code a bit. I made a array with the gray dates, then a variable with the green date. I never change the array, but update the green date on click, and then call a refresh on the datepicker:
$(function () {
var togo = [ '10/25/2013' ];
var redDates = [ '10/27/2013', '10/28/2013' ];
var grayDates = [ '10/24/2013', '10/25/2013', '10/26/2013' ];
var greenDate = '10/24/2013';
$("#datepicker").datepicker({
numberOfMonths: 2,
selectMultiple: true,
beforeShowDay: function (date) {
var theday = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
return [true, greenDate == theday ? "specialDate" : ($.inArray(theday, redDates) >= 0 ? "specialDate2" : ($.inArray(theday, grayDates) >= 0 ? "specialDate1" : ""))];
},
onSelect: function (dateStr) {
var date = new Date(dateStr);
var theday = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
if ($.inArray(theday, grayDates) >= 0) {
greenDate = theday;
}
$('#datepicker').datepicker("refresh");
}
});
});
I wasn't sure what togo was for, but this should get you started.
Demo: http://jsfiddle.net/xU47h/3/

Categories