JSP gets the server time in the form of a countdown displayed on the page

  • 2020-06-19 11:32:44
  • OfStack

 
<%@ 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(); 
// Gets the server time  
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}';// Gets the end or start time in the database, and then begins to calculate how much time is left before the start or end  
NowSecond = (parseInt(NowSecond)+1); 
//------------------------------------------------------------------- 
var a, a1, a2; 
var EndTime ; 
a = unescape('${enddate}').split(" "); // This is the comment, this sentence is the key to the accuracy of the countdown, adjust to other required time, for example 2012-12-20 And so on.  
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=" All time! "; 
} 
setTimeout("showtime()", 1000); 
} 
function startclock() { 
showtime(); // Call the compute time difference method and display  
} 
</script> 
</head> 
<body> 
<div id="CountMsg"> 
 There's still time to go <font color="red" size=5><strong id="RemainD">XX</strong> day <strong id="RemainH">XX</strong> when <strong id="RemainM">XX</strong> points <strong id="RemainS">XX</strong> seconds </font> 
</div> 
</body> 
</html> 

Related articles: