I’m hoping someone here can help me with the following: –
Currently, I have the following script for my datepicker snippet where I have excluded Sundays.
<script>
window.onload = function() {
if (window.jQuery) {
let $ = window.jQuery;
$(function() {
$("#date").datepicker({
dateFormat: 'dd/mm/yy',
minDate: +1,
maxDate: '+2M',
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 0), ''];
}
});
});
}
}
I would like to add the following conditions: –
- Exclude multiple specific dates (i.e. 16/04/21, etc.)
- Disable next business day selection by customers AFTER 12pm.
For point #2, I have the following code but am unsure of where to include it in the above: –
$("#date" ).datepicker({
minDate: +1,
beforeShow : function(){
var dateTime = new Date();
var hour = dateTime.getHours();
if(hour >= 12){
$(this).datepicker( "option", "minDate", "+2" );
}
}
Please let me know, thank you!
2
Answers
You could always use the tried and true MomentJS which has plenty of development history, including nice modules to deal with localization, internationalization, business days, holidays and the other aspects of temporal qualifications.
Note that you cannot do naive things like adding 2 to something in a loop. You have to remember to deal with things like time zones, and the possibility your customer is somewhere where the time to them is valid, but to Shopify code running on their servers, maybe differs.
Any results you get from MomentJS can then be used to feed some widget that displays a calendar and offers the options to black out certain dates.
Please refer below code snippet: