Check if time is between two values with hours and minutes in javascript
If its 14:04
your condition will fail as 4
is smaller 5
. The simplest would probably be to just take the full minutes of the day:
const start = 13 * 60 + 5;
const end = 19 * 60 + 57;
const date = new Date();
const now = date.getHours() * 60 + date.getMinutes();
if(start <= now && now <= end)
alert("in time");
check if "current time" is between 2 times. But also check for nights as the day before
You could use a simple function like this to convert your time to a number of minutes since 0:00:
function getMinutes(str) {
var time = str.split(':');
return time[0]*60+time[1]*1;
}
And a similar function to get the current time into the same form in order to compare:
function getMinutesNow() {
var timeNow = new Date();
return timeNow.getHours()*60+timeNow.getMinutes();
}
Then convert both opening and closing time and, if it happens that closing time is before opening time, add 24 hours to it.
var now = getMinutesNow();
var start = getMinutes('10:00');
var end = getMinutes('2:00');
if (start > end) end += getMinutes('24:00');
if ((now > start) && (now < end)) { // your code here
Check if current time is between start time & end time in JavaScript
I think you want something like this..
checkTime();
function checkTime() {
var date = new Date(); // current time
var hours = date.getHours();
var mins = date.getMinutes();
var day = date.getDay();
var totalMins = (hours * 60) + mins;
var targetMins = 20 * 60
var remainMins = targetMins - totalMins
if(hours >= 11 && hours < 20) {
document.getElementById("text").innerHTML = "Yes Current time is between 11am to 8pm And " + remainMins + " mins left to be time 8pm";
console.log("Yes Current time is between 11am to 8pm And " + remainMins + " mins left to be time 8pm");
}
else {
document.getElementById("text").innerHTML = "There is " + remainMins + " mins left to be time 8pm";
console.log("There is " + remainMins + " mins left to be time 8pm");
}
}
<p id="text"></p>
Checking if time is between two times in JS
If you don't mind an extra library you can try moment.js using moment isBetween method.
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25');
Now you said you have a database and you are querying the WHOLE database and checking, this is inefficient. Databases optimize queries and checks for you. You should let MySQL handle it for you, for example here:
SELECT * FROM `replay` WHERE (date_field BETWEEN '2016-01-30 14:15:55' AND '2016-09-29 10:15:55')
Do not load the entire database and check it on the app.
Now just for the sake of the question to check if one date is between other two you can simply compare the dates.
var a = new Date(SOME_DATE)
var b = new Date(OTHER_DATE)
var c = new Date(DATE_TO_CHECK)
var isBetween = c>=a && c<=b || c>=b && c<=a
How to check if current time is between two given time stored in Array
The following function gives you the name of the current shift, but you can also pass a moment.js object to get the shift of a specific time.
For the edge cases that are part of multiple shifts (like "7:00", "15:00", ...) my script returns the first found shift.
function getShiftName(time = moment()) {
const isBetween = (a, b, c) => ((a <= b && b <= c) || a > c && (a <= b || b <= c));
for (shift of Time) {
if (isBetween(shift.startTime, time.format("HH:mm"), shift.endTime)) {
return shift.shiftName;
}
}
return "none";
}
// your sample data, minified for a shorter code-block
var Time = [{"shiftName": "Shift A", "shiftCode": "shiftA", "startTime": "07:00", "endTime": "15:00"},{ "shiftName": "Shift B", "shiftCode": "shiftB", "startTime": "15:00", "endTime": "23:00", }, { "shiftName": "Shift C", "shiftCode": "shiftC", "startTime": "23:00", "endTime": "07:00", }];
// demo
console.log("now", getShiftName()); // the current shift name
console.log(" 12", getShiftName(moment(12,"HH"))); // shift A
console.log(" 18", getShiftName(moment(18,"HH"))); // shift B
console.log(" 23", getShiftName(moment(23,"HH"))); // shift B
console.log(" 24", getShiftName(moment(24,"HH"))); // shift C
console.log(" 4", getShiftName(moment( 4,"HH"))); // shift C
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
Related Topics
How to Loop Through Json Array and Get Specific Values
How to Clear Browsing History Using JavaScript
Detect If a Field Is Updated With JavaScript or Jquery
How to Set a Header Field on Post a Form
How to Change Text Value Upon Button Press in React Native
How to Allow Only English Letters in Input Fields
Display HTML Form Values in Same Page After Submit Using Ajax
How to Get Dynamic Id With Javascript/Jquery from PHP Loop
Removing Number from Notification Icon
Regex in JavaScript Allow Only Numbers and One Dot Followed by Max 2 Number
How to Include External JavaScript Library in Reactjs
Moment Js Get First and Last Day of Current Month
Postman: How to Check Whether the Field Is Returning Null in the Postman Automation
Generate Pdf from HTML in Div Using JavaScript
Is It Any Limit for Post Data Size in Ajax