I am doing a Website for Restaurants Home Delivery ,depending on Restaurant\'s Home Delivery Timings i need to enable / disable Order Now Button
I have got startTim
Doesn't seem to work with times passing midnight, try changing the time from 6:30PM to 2:30AM. A good solution is to use momentjs with the moment-range plugin
function inTimeRange(time, startTime, endTime)
{
//Setup today vars
var today = new moment(new Date());
var ayear = today.year();
var amonth = today.month() + 1; // 0 to 11
var adate = today.date();
amonth = String(amonth).length < 2 ? "0" + amonth : amonth;
adate = String(adate).length < 2 ? "0" + adate : adate;
//Create moment objects
var moment1, moment2;
var temp = endTime.split(" ");
if(temp[1].toLowerCase() == "am")
{
var test1 = ayear + "-" + amonth + "-" + adate + " " + startTime;
var test2 = ayear + "-" + amonth + "-" + adate + " " + endTime;
//Make sure that both times aren't morning times
if(moment(test2).isAfter(test1))
{
var moment1String = ayear + "-" + amonth + "-" + adate + " " + startTime;
var moment2String = ayear + "-" + amonth + "-" + adate + " " + endTime;
}
else
{
var moment1String = ayear + "-" + amonth + "-" + adate + " " + startTime;
var moment2String = ayear + "-" + amonth + "-" + (adate + 1) + " " + endTime;
}
moment1 = moment(moment1String, "YYYY-MM-DD HH:mm A");
moment2 = moment(moment2String, "YYYY-MM-DD HH:mm A");
}
else
{
var moment1String = ayear + "-" + amonth + "-" + adate + " " + startTime;
var moment2String = ayear + "-" + amonth + "-" + adate + " " + endTime;
moment1 = moment(moment1String, "YYYY-MM-DD HH:mm A");
moment2 = moment(moment2String, "YYYY-MM-DD HH:mm A");
}
//Run check
var start = moment1.toDate();
var end = moment2.toDate();
var when;
if(String(time).toLowerCase() == "now")
{
when = moment(new Date());
}
else
{
var timeMoment1String = ayear + "-" + amonth + "-" + adate + " " + time;
when = moment(timeMoment1String);
}
var range = moment().range(start, end);
return when.within(range);
}
I have done something similar to this. My app was receiving times in military time. So in the case where times didn't pass into the next day (i.e. start time of 09:00 and end time of 17:00 you would just check if you are between those times.
In the case of the end time being after midnight (i.e. start time of 15:00 and end time of 01:00) then there are three cases:
Here is my code sample:
const isCurrentDayPart = (dayPart) => {
let currentTime = moment();
let startTime = moment(dayPart.startTime, "HH:mm");
let endTime = moment(dayPart.endTime, "HH:mm");
if (dayPart.startTime < dayPart.endTime) {
return currentTime.isBetween(startTime, endTime);
} else {
if (currentTime.isAfter(endTime) && currentTime.isAfter(startTime)) {
return true;
} else if (currentTime.isBefore(endTime) && currentTime.isBefore(startTime)) {
return true;
}
}
return false;
};
var startTime = '02:30 AM';
var endTime = '13:00 PM';
var now = new Date();
var startDate = dateObj(startTime);
var endDate = dateObj(endTime);
alert(endDate)
var open = now < endDate && now > startDate ? 'open' : 'closed';
alert('Restaurant is ' + open);
function dateObj(d) {
var parts = d.split(/:|\s/),
date = new Date();
if (parts.pop().toLowerCase() == 'pm') parts[0] = (+parts[0]) + 12;
date.setHours(+parts.shift());
date.setMinutes(+parts.shift());
return date;
}
var startTime = '8:30 AM';
var endTime = '6:30 PM';
var now = new Date();
var startDate = dateObj(startTime);
var endDate = dateObj(endTime);
var open = now < endDate && now > startDate ? 'open' : 'closed';
alert('Restaurant is ' + open);
function dateObj(d) {
var parts = d.split(/:|\s/),
date = new Date();
if (parts.pop().toLowerCase() == 'pm') parts[0] = (+parts[0]) + 12;
date.setHours(+parts.shift());
date.setMinutes(+parts.shift());
return date;
}
Something like this should work
var startTime = '6:30 PM';
var endTime = '8:30 AM';
var now = new Date();
var startDate = dateObj(startTime); // get date objects
var endDate = dateObj(endTime);
if (startDate > endDate) { // check if start comes before end
var temp = startDate; // if so, assume it's across midnight
startDate = endDate; // and swap the dates
endDate = temp;
}
var open = now < endDate && now > startDate ? 'open' : 'closed'; // compare
console.log('Restaurant is ' + open);
function dateObj(d) { // date parser ...
var parts = d.split(/:|\s/),
date = new Date();
if (parts.pop().toLowerCase() == 'pm') parts[0] = (+parts[0]) + 12;
date.setHours(+parts.shift());
date.setMinutes(+parts.shift());
return date;
}
.as-console-wrapper {top : 0!important}
It splits those times and adds 12 to PM times, then creates date objects that can easily be compared.