0

我想用 Jquery 检查当前时间是否在上午 10.30 到上午 11.30 之间。我将如何以正确的方式做到这一点?

我尝试了以下方式,但它没有按我的预期工作

function compareTime(){

  var d = new Date(), // current time
  hours = d.getHours(),// current hour
  mins = d.getMinutes(); // current minute

  var sTime = "10.30";
  var eTime = "11.30";
  var cTime = hours+'.'+mins;

  if(sTime < cTime < eTime){
     // here i want to do something if the current time is in between ...
  }else{

  }
}
4

2 回答 2

4

与jQuery无关:

function checkTime(d) {
  d = d || new Date();
  var minTime = new Date(d);
  var maxTime = new Date(d);

  minTime.setHours(10, 30, 0, 0);
  maxTime.setHours(11, 30, 0, 0);

  return d > minTime && d < maxTime;
}
于 2012-09-19T06:51:09.887 回答
0

你可以这样做,

var sTime = "10.30"
sTime = sTime.split("."); 
var eTime = "11.30";
eTime = eTime.split("."); 

这样 eTime 和 sTime 将成为一个数组sTime[0] = "10"sTime[1] = "30",同样 eTime 也是。现在开发一个循环来与hours和进行比较mins

于 2012-09-19T06:57:49.533 回答