js 判断所选时间(或者当前时间)是否在某一时间段用于不同时间段显示不同的客服qq

//qq交谈
var nowtimes= new Date();
var starttimes="16:30";
var endtimes="23:59";
var timestrings=nowtimes.getHours()+":"+nowtimes.getMinutes();
 function qqchats1(){
     if(!time_range (starttimes,endtimes,timestrings)){
       var list = new Array("2885794580","3001017451");   
     }
     else
     {
        var list = new Array("2885794572","2885794571");         
     }
        var i = fRandomBy(0,1);
        window.location.href = "tencent://message/?uin="+list[i]+"&Site=&menu=yes";
}function fRandomBy(under, over){ 
   switch(arguments.length){ 
     case 1: return parseInt(Math.random()*under+1); 
     case 2: return parseInt(Math.random()*(over-under+1) + under); 
     default: return 0; 
   }
}   

var time_range = function (beginTime, endTime, nowTime) {
      var strb = beginTime.split (":");
      if (strb.length != 2) {
          return false;
      }
  
      var stre = endTime.split (":");
      if (stre.length != 2) {
          return false;
     }
 
     var strn = nowTime.split (":");
     if (stre.length != 2) {
         return false;
     }
     var b = new Date ();
     var e = new Date ();
     var n = new Date ();
 
     b.setHours (strb[0]);
     b.setMinutes (strb[1]);
     e.setHours (stre[0]);
     e.setMinutes (stre[1]);
     n.setHours (strn[0]);
     n.setMinutes (strn[1]);

    if (n.getTime () - b.getTime () > 0 && n.getTime () - e.getTime () < 0) {
        console.log("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",");
         return true;
     } else {
        console.log("当前时间是:" + n.getHours () + ":" + n.getMinutes () + ",不在该时间范围内!");
         return false;
    }
 }
原文地址:https://www.cnblogs.com/mumublog/p/6062037.html