欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-27 16:34:34
复制代码 代码如下: <%@ page language="java" import="java.util.*"%>
复制代码 代码如下:

<%@ 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>