简体   繁体   English

如何使用 jquery/javascript 检查当前时间是否在工作日的特定范围内?

[英]How to check if current time falls within a specific range on a week day using jquery/javascript?

When a user visits a page I need to check if the current time falls between 9:00 am and 5:30pm on a weekday and display something using jquery/javascript.But I am not sure how can check that.当用户访问页面时,我需要检查当前时间是否在工作日的上午 9:00 到下午 5:30 之间,并使用 jquery/javascript 显示一些内容。但我不确定如何检查。

Could someone please help?有人可以帮忙吗?

Thanks谢谢

This should hopefully help:这应该会有所帮助:

function checkTime() {
    var d = new Date(); // current time
    var hours = d.getHours();
    var mins = d.getMinutes();
    var day = d.getDay();

    return day >= 1
        && day <= 5
        && hours >= 9 
        && (hours < 17 || hours === 17 && mins <= 30);
}

Just added two cents to the hour checking...只是在小时检查中增加了两美分......

I personally think that a better way would be to check if is in the range of a:b--c:d...我个人认为更好的方法是检查是否在 a:b--c:d 的范围内...

 // check if h:m is in the range of a:bc:d // does not support over-night checking like 23:00-1:00am function checkTime (h,m,a,b,c,d){ if (a > c || ((a == c) && (b > d))) { // not a valid input } else { if (h > a && h < c) { return true; } else if (h == a && m >= b) { return true; } else if (h == c && m <= d) { return true; } else { return false; } } }

Using the current time you could do something like this:使用当前时间,您可以执行以下操作:

var now = new Date(),
    day = now.getDay(),
    hours = now.getHours();

//Check if day is Mon-Fri
if(0 < day < 6) {
  //check between 9am and 5pm
  if(9 <= hours <= 17) {
     if(hours !== 17 || now.getMinutes() <= 30) {
          //your jQuery code here
     }
  }
}
var now = new Date();
var dayOfWeek = now.getDay();
if(dayOfWeek > 0 && dayOfWeek < 6){
   //falls on a weekday
   if (now.getHours() > 9 && (now.getHours() < 17 && now.getMinutes() < 30)) {
      //it's in schedule
   }
}

I had a requirement where I needed to find if time in one date object lies within other two.我有一个要求,我需要找出一个日期对象中的时间是否在其他两个对象中。 (Date didn't matter, instead only time). (日期无关紧要,而只是时间)。

I did it this way, I thought it might help someone.我是这样做的,我认为它可能会帮助某人。

/*
* d, start and end all are Date objects.
*/
const checkIfTimeIsInRange = (d, start, end) => {
    if (start.getHours() < d.getHours() && d.getHours() < end.getHours()) {
        return true;
    } else if (start.getHours() == d.getHours()) {
        if (d.getHours() == end.getHours()) {
            if(start.getMinutes() <= d.getMinutes() && d.getMinutes() <= end.getMinutes()) {    
                return true
            } else {
                return false
            }
        } else if(start.getMinutes() <= d.getMinutes()) {
            return true
        } else {
            return false
        }
    } else if (d.getHours() == end.getHours()) {
        if(d.getMinutes() <= end.getMinutes()) {
            return true
        } else {
            return false
        }
    } else {
        return false
    }
}

PS Any improvements, suggestions are welcome. PS任何改进,欢迎提出建议。

 document.getElementById('tmp_button-48523').addEventListener('click', function() { let d = new Date(); let localTime = d.getTime(); let localOffset = d.getTimezoneOffset() * 60000; let utc = localTime + localOffset; let target_offset = -7;//PST from UTC 7 hours behind right now, will need to fix for daylight let los_angles = utc+(3600000*target_offset); nd = new Date(los_angles); let current_day = nd.getDay(); let hours = nd.getHours(); let mins = nd.getMinutes(); alert("los_angles time is " + nd.toLocaleString()); alert("Day is "+current_day); if(current_day==2 && hours >= 14 && hours <=15 ) if(hours!=15 && mins >= 32) fbq('track', 'LT-Login'); }, false); function fbq(p1,p2){ alert(p1); }
 <button id="tmp_button-48523"> Click me! </button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 考虑到分钟,如何检查当前时间是否在特定范围内 - How to check if current time falls within a specific range considering also minutes 检查当前时间是否在这个范围内? - Check if the current time falls in this range? Jquery / Javascript允许在一周中特定时间内的特定网页 - Jquery/Javascript to allow a specific webpage in a specific time of the day in the week 将服务器时间转换为东部时间,并检查时间是否在javascript中的几个时间范围内 - Convert server time to eastern time and check if time falls within a few time range in javascript 如何使用jquery / js检查时间范围是否大于7周的时间范围 - How to check if a time range is greater than a 7 week time range using jquery/js 尝试使用momentJS(UTC)检查当前时间是否在开始/结束时间范围内 - Trying to check the current time falls inside a range of start/end time using momentJS (UTC) 使用jQuery检查当前时间是否在时间范围内(24hr) - Checking if current time falls in a time range (24hr) using jQuery JavaScript-Moment.js-isBetween()方法检查时间是否在一定范围内 - JavaScript - Moment.js - isBetween() method check if time falls in a range 如何检查日期时间是否在范围之间? jQuery或javascript中的YYYY-MM-DD HH:MM:SS格式 - How to check datetime if it falls between a range? YYYY-MM-DD HH:MM:SS format in jquery or javascript 如何在Javascript中检查选定的时间是否在给定的时间范围内 - How to check selected time is within the given time range in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM