In the FullCalendar plugin, I need allow selection of days until a day or between dates. I put an example to explain better.
https://codepen.io/stefanmalex/pen/Jjjjgmp
I have an array with disallowed days:
var disallowedDays = ['2019-10-17', '2019-10-23', '2019-10-26']
I added the 'selectAllow' callback:
selectAllow: function (selectInfo) {
if (disallowedDays.includes(selectInfo.startStr)) {
return false;
}
return true;
}
This works perfectly if you select day per day, allows selection of all days less disallowed days in array.
PROBLEM: When you select multiple days, it allows select disallowed days. (Example: select from '2019-10-15' to '2019-10-26').
What I need, example:
If the selection starts on '2019-10-11', it has to allows you to select until '2019-10-16' because next day ('2019-10-17') is disallowed.
I let the example on codepen. https://codepen.io/stefanmalex/pen/Jjjjgmp
ADyson has recognized it correctly.
The program logic needs to be changed.
In the selectAllow you were checking the array with startStr, so basically it will be checking with start date of selection only, not the whole selection.
So, if you tried to select 14 oct to 18 oct, you needed to check / compare the disallowed dates with in this range.
So, it is needed to loop through the disallowedDays array to check each date within the tried selection, like the following loop:
for(var i=0;i<disallowedDays.length;i++) {
var dd = new Date(disallowedDays[i]);
if(dd.getTime() >= startDate.getTime() && dd.getTime() <= endDate.getTime()){
return true;
}
}
Following this logic, check here the solution you might be expecting
Related
converting the text box value to date on change event with a two digit year is being considered as the current year
I am clearing out my text box if the user enters a date equal to the current date.
I have a text box to take a date (06-13-1999) / (06/13/1999) formats. Before I finish entering my 4 digit year since I am using an on Change event Javascript native date function is converting the 2 digit year to current year and clearing my text box.
So I am currently using a condition by splitting the date entered date.split('/')[2] .length = 4 which seems to be tedious since now I need two split functions to allow different date formats.
if (dob.split('/')[2].length == 4 || dob.split('-')[2].length == 4)
{
ndob = new Date(dob);
ndob = formatdate(ndob);
}
Is there any other way of handling this on change event
You can use regex to handle both cases at the same time:
if (dob.split(/\/|-/).length === 4) {
You can check the length of your textbox val, if it is 10 then only execute your code.
document.querySelector('input').addEventListener('keyup', function() {
const val = this.value;
if(val.length===10) {
console.log('valid date');
}
})
<input type="text">
I'm using the Foundry template on Squarespace and I need to change the date format on post pages from english to portuguese. Instead of "May 6" I need "6 Mai". In Brazil we use the pattern dd/mm/yyyy. In this case I just want the day and month, and also translate all the months (to: Jan, Fev, Mar, Abr, Mai, Jun, Jul, Ago, Set, Out, Nov, Dez).
I already saw people solving this for others languages there. But not to portuguese or on the Foundry template. It's possible to make a code-injection on Squarespace, on the head or footer. I just need a Javascript that can do that, overwriting the theme's default date format.
I would approach it via the following Javascript, inserted via code injection. Note that although some of the month abbreviations are the same, I've included them for clarity and so that it may be more reusable for others. Also, the abbreviations I've used for the keys (that is, the original month abbreviations) may not be what Squarespace actually uses, so they may need to be updated.
<script>
(function() {
var dates = document.getElementsByClassName("dt-published date-highlight");
var newDate;
var i,I;
// Create object with 'source' keys on the left, and 'output' values on the right.
var months = {
"Jan":"Jan",
"Feb":"Fev",
"Mar":"Mar",
"Apr":"Abr",
"May":"Mai",
"Jun":"Jun",
"Jul":"Jul",
"Aug":"Ago",
"Sep":"Set",
"Oct":"Out",
"Nov":"Nov",
"Dec":"Dez"
};
// Loop through all dates, replacing months and reordering display.
// - Trim extra white space from beginning and end of date.
// - Replace multiple consecutive spaces with a single space.
// - Split by space into an array.
// - Replace month text based on 'months' object key:value pairs.
// - Convert array to string, rearranging display order of elements.
// - Set new date HTML.
for (i=0, I=dates.length; i<I; i++) {
newDate = dates[i].innerHTML.trim();
newDate = newDate = newDate.replace(/ +/g, ' ');
newDate = newDate.split(" ");
newDate[0] = months[newDate[0]];
newDate = newDate[1] + " " + newDate[0];
dates[i].innerHTML = newDate;
}
})();
</script>
I want to add leading Zeros to these single digit days in FullCalendar month view.
What I want is :
Means, 3 as 03, 4 as 04 and so on..
Years later I could not find an option to set the day-number format to one with a leading zero, i did it the same way but without JavaScript in Fullcalendar 5.4.0:
The classname in my case was .fc-daygrid-day-number, I think thats implements the type of the initialized view, whats in my case initialized with: {initialView: 'dayGridMonth'}
Array.prototype.forEach.call(
document.querySelectorAll('.fc-daygrid-day-number'),
function(el) {
if (el.innerText.length === 1) {
el.innerText = '0' + el.innerText;
}
}
)
There doesn't appear to be an option for this in the fullCalendar options curently. Without modifying the fullCalendar source, the best I could come up with is this. If you look at the rendered calendar HTML, you'll see that each day number is wrapped in a <td> with the CSS class fc-day-number. So we can modify the contents of the <td>. Put this code directly after your calendar initialisation code:
$('.fc-day-number').each(function() {
var day = $(this).html(); //get the contents of the td
//for any fields where the content is one character long, add a leading zero
if (day.length == 1)
{
$(this).html("0" + day);
}
});
I had the same problem with React and find this question equivalent. Then I come up with this solution at Fullcalendar 5.6.0:
I used a prop called "dayCellContent" and passed a function to return the formatted string.
<FullCalendar
plugins={[dayGridPlugin]}
initialView="dayGridMonth"
dayCellContent={({ dayNumberText }) => (("00" + dayNumberText).substring(dayNumberText.length))}
/>
For example, I have a table with id tbBookList, and for each row (<tr>), the 4th column (<td>) shows a date. I want to remove the all the row with the Date before this year (such as 2016); The Date format is yyyy-MM-dd such as 2016-01-24.
I google it but I found I'm hard to describe my question.
The fast way I know is that I can select all row with $("#tbBookList>tr") then I can loop it. However, I want to know if there is a better way such as using jquery selector to do that.
You can just use filter():
$('#tbBookList tr').filter(function() {
var cell = $(this).find('td').eq(3);
var date = cell.text();
var year = date.split('-').shift();
return year < 2016;
}).remove();
No matter what you do a loop of all the rows has to occur.
For something like this I would use filter() to do the loop rather than each but that is personal preference
$("#tbBookList>tr").filter(function(){
var date = $(this).find('td').eq(2).text();
return SomeTestOfDateValue // boolean
}).remove();
Im using the car rental plugin and need to modify it in a way that if the rental time chosen by customer is less than 2 hours, to give him a message, pop up or any kind of message, that he needs to choose time minimum 2 hours for rental.
You can see the example here: http://envato.bestsoftinc.net/wp-car/
I need to make sure that there is at least 2 hour difference between pick up date field and drop off date field, if not, I need to show him message and not let him click on the Search Button. Any ideas how I can achieve that with jQuery or Regular Javascript please?
Thank you
this is the basic logic for it, try implement this with your site.
i found moment.js is really helpful with js time date obj you can give it a try
if($('#checkInDate').value() === $('#checkOutDate').value) [
if both date is the same date, than
var checkInTime = $('#checkInTime').value();
var checkOutTime = $('#checkInTime').valeu();
get time value
if(checkOutTime > checkInTime) {
checkOutTime must be later than checkInTime when it's the same date
if(checkOutTime - checkInTime > 2) {
if duration is more than 2 than this value is ok
alert('ok');
this fail 3rd if statement
} else { alert('error must less than 2 '); }
this fail 2nd if statement
} else {alert('error checkout must bigger than checkin'); }
end 1st if statement that check for same date
}