JSP获取服务器时间以倒计时的形式在页面显示

时间:2021-05-26

复制代码 代码如下:
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://mons/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>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章