如果满足日期条件,如何运行JavaScript代码

时间:2014-06-10 20:25:00

标签: javascript html html5 criteria

如果符合日期条件,我想找到运行一段javascript代码的方法:

例如:

  • 如果日期介于June 6th, 2014 5PM CSTJune 16th, 2014 5PM CST之间,请不要运行代码。

或者我猜相反的可能更精确。

  • June 6th, 2014 5PM CST至2014年6月16日下午5点之间运行代码AND 2014年6月20日下午5点CST and 2014年6月25日下午5点。

任何想法或例子?

3 个答案:

答案 0 :(得分:1)

为什么不简单地将代码放在if语句中?

//create current Date
var curDate = new Date();
//This date represents the upper limit of the interval
var upperDate = new Date('June 20, 2014 05:00 PM');
//This date represents the lower limit of the interval
var lowerDate = new Date('June 6, 2015 05:00 PM');
//Timezone settings on the client should be correct for this comparison to work
if(lowerDate > curDate || upperDate < curDate) {
//Your code here
}

答案 1 :(得分:0)

最直接的方法是运行轮询功能。

setInterval(poll, 1000); //interval is in milliseconds, so this will run poll() function every second.

function poll(){
  if (your_date_conditions){
     a_funciton_you_want_to_run(); 
  }
}

答案 2 :(得分:-1)

如果你想根据某个标准执行代码...这对我来说听起来像是一个if语句。

我不确定你还有什么意思。