Below is the information you requested. The day
represents the day you want to check, while open
and close
refer to the first and last open days of the week.
In this code snippet, I'm utilizing moment.js to convert text-based day names into numerical values. However, you can easily adapt this to work with arrays or other data structures.
This implementation is a modified version of a solution found here: Test if number is inside circular interval
The logic is simple - if the open day is less than or equal to the close day, it's a standard range check. You need to verify if the day falls between the open and close days to determine if it's open on that day.
If the open day is greater than the close day, it indicates a wrap-around range. In this case, you must check if the day is either greater than or equal to the open day OR less than or equal to the close day to ascertain its open status.
function isStoreOpen(day, open, close) {
const dayNum = moment().day(day).day();
const openNum = moment().day(open).day();
const closeNum = moment().day(close).day();
if(openNum <= closeNum) {
return (dayNum >= openNum && dayNum <= closeNum);
} else {
return (dayNum >= openNum || dayNum <= closeNum);
}
}
// Example for Store A with WORKING_DAYS = Tue - Sun
console.log("Store A (Tue - Sun) is open on Wednesday: " + isStoreOpen("Wednesday", "Tuesday", "Sunday"));
// Example for Store B with WORKING_DAYS = Mon - Fri
console.log("Store B (Mon - Fri) is open on Wednesday: " + isStoreOpen("Wednesday", "Monday", "Friday"));
// Example for Store C with WORKING_DAYS = Mon - Tue
console.log("Store C (Mon - Tue) is open on Wednesday: " + isStoreOpen("Wednesday", "Monday", "Tuesday"));
// Example for Store D with WORKING_DAYS = Fri - Mon
console.log("Store D (Fri - Mon) is open on Wednesday: " + isStoreOpen("Wednesday", "Friday", "Monday"));
console.log("Store D (Fri - Mon) is open on Saturday: " + isStoreOpen("Saturday", "Friday", "Monday"));
console.log("Store D (Fri - Mon) is open on Friday: " + isStoreOpen("Friday", "Friday", "Monday"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>
Here is a scenario more closely related to your specific use case:
function isStoreOpen(currentDay, dayStart, dayEnd) {
if (dayStart <= dayEnd) {
if (currentDay >= dayStart && currentDay <= dayEnd) {
return "open"
} else {
return "closed"
}
} else {
if (currentDay >= dayStart || currentDay <= dayEnd) {
return "open"
} else {
return "closed"
}
}
}
// Example for Store A with WORKING_DAYS = Tue - Sun
console.log("Store A (Tue - Sun) status on Wednesday: " + isStoreOpen(4, 2, 0));
// Example for Store B with WORKING_DAYS = Mon - Fri
console.log("Store B (Mon - Fri) status on Wednesday: " + isStoreOpen(4, 1, 5));
// Example for Store C with WORKING_DAYS = Mon - Tue
console.log("Store C (Mon - Tue) status open on Wednesday: " + isStoreOpen(4, 1, 2));
// Example for Store D with WORKING_DAYS = Fri - Mon
console.log("Store D (Fri - Mon) status on Wednesday: " + isStoreOpen(4, 5, 1));
console.log("Store D (Fri - Mon) status on Saturday: " + isStoreOpen(6, 5, 1));
console.log("Store D (Fri - Mon) status on Friday: " + isStoreOpen(5, 5, 1));
Your actual implementation could resemble something like this:
if (dayStart <= dayEnd) {
if (currentDay >= dayStart && currentDay <= dayEnd) {
return(<Text style={[styles.h4,styles.tag, {backgroundColor:'#4eae5c'}]}>open</Text>);
} else {
return(<Text style={[styles.h4,styles.tag,{backgroundColor:'red'}]}>closed</Text>);
}
} else {
if (currentDay >= dayStart || currentDay <= dayEnd) {
return(<Text style={[styles.h4,styles.tag, {backgroundColor:'#4eae5c'}]}>open</Text>);
} else {
return(<Text style={[styles.h4,styles.tag,{backgroundColor:'red'}]}>closed</Text>);
}
}