1

这是我的代码,

<script type="text/javascript">
function today(hour, minute) {
var now = new Date();
now.setHours(hour);
now.setMinutes(minute);
now.setSeconds(0);
now.setMilliseconds(0);
return now.getTime();
}

var now = new Date();
now = now.getTime();

if(
(now > today(11, 45) && now < today(14, 00)) ||
(now > friday(19, 00) && now < saturday(0, 00))
) {
alert("Lunchtime Specials!");
window.location = "http://google.ie";
}</script>

它适用于“今天”,但不适用于周五或周六等有什么建议吗?

4

1 回答 1

1

您只想检查今天是哪一天,然后是什么时间,以获取星期五的星期几:

var d = new Date();
var day = d.getDay();

if(day == 5){
alert("It's Friday!");
}

getDay()返回星期几(从 0-星期日到 6-星期六)

如果要检查一小时或一分钟,请执行以下操作(从顶部代码开始):

var hour = d.getHours();

if(hour == 11){
alert("It's 11 AM! Woo");
}

有关日期对象的完整文档,请参见此处:
http ://www.tizag.com/javascriptT/javascriptdate.php

于 2012-04-12T12:38:41.627 回答