1,前言
這個是根據網上一些資料重新編寫整理而來的,主要是作為自己使用的一個小積累,原本的目的是使網頁在規定的時間段內自動跳轉,在時間段外的請求不做處理;分別寫了JS和JSP的代碼,放在這裡以備查用;
2,代碼
2.1 倒計時模組
這個每隔1秒自動重新整理,可以稍作修改以作他用:
[javascript]
<!--倒計時-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
function Dclock()
{
var symbol="3月31日";
var deadline= new Date("03/31/2013 11:25:00"); //標準時間格式
var now = new Date();
var diff = -480 - now.getTimezoneOffset(); //是北京時間和當地時間的時間差
var leave = (deadline.getTime() - now.getTime()) + diff*60000; //getTime()可以取得1970之後的毫秒數,1970年前為負數
var day = Math.floor(leave / (1000 * 60 * 60 * 24)); //天數
var hour = Math.floor(leave / (1000*3600)) - (day * 24);
var minute = Math.floor(leave / (1000*60)) - (day * 24 *60) - (hour * 60);
var second = Math.floor(leave / (1000)) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);
if (now.getTime()<deadline.getTime())
{
LiveClock.innerHTML = "現在離"+symbol+"還有"+day+"天"+hour+"小時"+minute+"分"+second +"秒" ;
test.innerHTML= "getTime:"+now.getTime()+"Deadline:"+deadline.getTime();
setTimeout("Dclock()",1000); //調用自身每隔1秒
}
else if (now.getTime()>deadline.getTime())
{
LiveClock.innerHTML = "END";
setTimeout("Dclock()",1000);
}
}
</script>
<!--倒計時-->
<body onload=Dclock()>
<div id= LiveClock></div>
<div id=test></div>
</body>
<!--倒計時-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
function Dclock()
{
var symbol="3月31日";
var deadline= new Date("03/31/2013 11:25:00"); //標準時間格式
var now = new Date();
var diff = -480 - now.getTimezoneOffset(); //是北京時間和當地時間的時間差
var leave = (deadline.getTime() - now.getTime()) + diff*60000; //getTime()可以取得1970之後的毫秒數,1970年前為負數
var day = Math.floor(leave / (1000 * 60 * 60 * 24)); //天數
var hour = Math.floor(leave / (1000*3600)) - (day * 24);
var minute = Math.floor(leave / (1000*60)) - (day * 24 *60) - (hour * 60);
var second = Math.floor(leave / (1000)) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60);
if (now.getTime()<deadline.getTime())
{
LiveClock.innerHTML = "現在離"+symbol+"還有"+day+"天"+hour+"小時"+minute+"分"+second +"秒" ;
test.innerHTML= "getTime:"+now.getTime()+"Deadline:"+deadline.getTime();
setTimeout("Dclock()",1000); //調用自身每隔1秒
}
else if (now.getTime()>deadline.getTime())
{
LiveClock.innerHTML = "END";
setTimeout("Dclock()",1000);
}
}
</script>
<!--倒計時-->
<body onload=Dclock()>
<div id= LiveClock></div>
<div id=test></div>
</body> 2.2 JS跳轉
[javascript]
<!--Javascript begin-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
function Dclock()
{
var deadline= new Date("05/31/2012 20:30:00"); //記時起點
var deadline_2= new Date("05/31/2012 21:30:00"); //記時結束
var symbol="5月31日";
var now = new Date();
if (now.getTime()<deadline.getTime())
{
}
else if (deadline.getTime()<now.getTime()&&now.getTime()<deadline_2.getTime())//C語言寫慣了a<b<c了把
{
//alert(now.getTime());
window.location.href="http://localhost/XXX/page.html";
setTimeout("Dclock()",1000);
}
else if (now.getTime()>deadline_2.getTime())
{
}
}
</script>
<Javascript end-->
<!--Javascript begin-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript">
function Dclock()
{
var deadline= new Date("05/31/2012 20:30:00"); //記時起點
var deadline_2= new Date("05/31/2012 21:30:00"); //記時結束
var symbol="5月31日";
var now = new Date();
if (now.getTime()<deadline.getTime())
{
}
else if (deadline.getTime()<now.getTime()&&now.getTime()<deadline_2.getTime())//C語言寫慣了a<b<c了把
{
//alert(now.getTime());
window.location.href="http://localhost/XXX/page.html";
setTimeout("Dclock()",1000);
}
else if (now.getTime()>deadline_2.getTime())
{
}
}
</script>
<Javascript end-->
2.3 JSP代碼
由於JS取得是用戶端時間,雖然可以通過互動來取得伺服器時間,但是顯得有些多餘,這裡貼一下JSP的代碼
[java]
<%@ page contentType="text/html; charset=UTF-8" import="java.util.*;" %>
<%//擷取目前時間
java.util.Date now=new java.util.Date();
java.util.Date dline=new java.util.Date("03/01/2012 20:30:00");
java.util.Date dline2=new java.util.Date("03/03/2012 21:30:00");
if (now.getTime()<dline.getTime())
{
}
else if (dline.getTime()<now.getTime()&&now.getTime()<dline2.getTime())//C語言寫慣了a<b<c了把
{
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn="http://XXX/page/earthhour/";
response.setHeader("Location",newLocn);
}
else if (now.getTime()>dline2.getTime())
{
}
%>
摘自 DesignLab