JSP擷取伺服器時間以倒計時的形式在頁面顯示_JSP編程

來源:互聯網
上載者:User
複製代碼 代碼如下:

<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/commons/meta.jsp"%>
<%@ include file="/commons/meta-et.jsp"%>
<%
Calendar cal = Calendar.getInstance();
int hour = cal.get(cal.HOUR_OF_DAY);
int minute = cal.get(cal.MINUTE);
int month = cal.get(cal.MONTH);
int date = cal.get(cal.DATE);
int year = cal.get(cal.YEAR);
int second = cal.get(cal.SECOND);
%>
<script type="text/javascript" defer="defer">
var NowHour = "";//Today.getHours();
var NowMinute = "";//Today.getMinutes();
var NowMonth = "";//Today.getMonth();
var NowDate = "";//Today.getDate();
var NowYear = "";//Today.getYear();
var NowSecond = "";//Today.getSeconds();
//擷取服務端時間
function getTime(){
NowHour = <%=hour%>;
NowMinute = <%=minute%>;
NowMonth = <%=month%>;
//NowMonth = (parseInt(NowMonth)+1);
NowDate = <%=date%>;
NowYear = <%=year%>;
NowSecond = <%=second%>;
}
getTime();
startclock();
var timerID = null;
var timerRunning = false;
function showtime() {

var time='${enddate}';//擷取資料庫中結束時間或開始時間,然後開始計算離開始或結束還差多少時間
NowSecond = (parseInt(NowSecond)+1);
//-------------------------------------------------------------------
var a, a1, a2;
var EndTime ;
a = unescape('${enddate}').split(" "); //這是注釋,此句是倒計時準確度的關鍵,調整成其他需要的時間即可,例如2012-12-20等。
if(a.length > 1){
a1 = a[0].split("-");
a2 = a[1].split(":");
EndTime= new Date(a1[0], a1[1] - 1, a1[2], a2[0], a2[1],a2[2]);
}
var NowTime= new Date(NowYear,NowMonth,NowDate,NowHour,NowMinute,NowSecond);
var nMS =EndTime.getTime() - NowTime.getTime();
var nD =Math.floor(nMS/(1000 * 60 * 60 * 24));
var nH=Math.floor(nMS/(1000*60*60)) % 24;
var nM=Math.floor(nMS/(1000*60)) % 60;
var nS=Math.floor(nMS/1000) % 60;
var dddd= nD+'-'+nH+'-'+nM+'-'+nS;

if(nD>= 0){
document.getElementById('RemainD').innerText = nD;
document.getElementById('RemainH').innerText = nH;
document.getElementById('RemainM').innerText = nM;
document.getElementById('RemainS').innerText = nS;
}else {
document.getElementById('CountMsg').innerText="所有時間到!";
}
setTimeout("showtime()", 1000);
}
function startclock() {
showtime(); //調用計算時間差方法,並顯示
}
</script>
</head>
<body>
<div id="CountMsg">
距離結束還剩<font color="red" size=5><strong id="RemainD">XX</strong>天<strong id="RemainH">XX</strong>時<strong id="RemainM">XX</strong>分<strong id="RemainS">XX</strong>秒</font>
</div>
</body>
</html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.