8

当用户访问一个页面时,我需要检查当前时间是否在工作日的上午 9:00 到下午 5:30 之间,并使用 jquery/javascript 显示一些内容。但我不确定如何检查。

有人可以帮忙吗?

谢谢

4

6 回答 6

20

这应该会有所帮助:

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);
}
于 2012-01-31T14:37:12.480 回答
3
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
   }
}
于 2012-01-31T14:37:25.520 回答
2

使用当前时间,您可以执行以下操作:

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
     }
  }
}
于 2012-01-31T14:37:22.400 回答
2

只是在小时检查中增加了两美分......

我个人认为更好的方法是检查是否在 a:b--c:d 的范围内...

// check if h:m is in the range of a:b-c: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;
           }
      }
}

于 2015-10-01T20:46:05.507 回答
0

我有一个要求,我需要找出一个日期对象中的时间是否在其他两个对象中。(日期无关紧要,而只是时间)。

我是这样做的,我认为它可能会对某人有所帮助。

/*
* 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任何改进,欢迎提出建议。

于 2020-02-19T13:10:47.487 回答
0

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>

于 2020-03-24T22:00:14.020 回答