秒杀倒计时实现 JS+jsp获取服务器时间

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
//引入工具包
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";


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">
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%>";//Today.getHours();
NowMinute = "<%=minute%>";//Today.getMinutes();
NowMonth = "<%=month%>";//Today.getMonth();
//NowMonth = (parseInt(NowMonth)+1);
NowDate = "<%=date%>";//Today.getDate();
NowYear = "<%=year%>";//Today.getYear();
NowSecond = "<%=second%>";//Today.getSeconds();
}

var yzm ;//验证码
getTime();
startclock();
var timerID = null;
var timerRunning = false;

var year = "";
var month= "";
var day= "";
var hour= "";
var minute= "";
var seconds= "";



function showtime() {
var time=$("#hiddenTime").val();//获取数据库中结束时间或开始时间,然后开始计算离开始或结束还差多少时间

splitTime(time);//以上用于截取时间,分成年,月,日,时,分,秒

//=========================================
//以下用于用于计算时间差
//Today = new Date();

//if (NowYear < 2000)
// NowYear = 1900 + NowYear;
//Today = null;
Hourleft = hour - NowHour;
Minuteleft = minute - NowMinute;
Secondleft = seconds - NowSecond;
Yearleft = year - NowYear;
Monthleft = month - NowMonth - 1;
Dateleft = day - NowDate;
NowSecond = (parseInt(NowSecond)+1);
//Secondleft = (parseInt(Secondleft)-1);
//秒钟
if (NowSecond > 60) {
NowSecond =  NowSecond - 60;
NowMinute = (parseInt(NowMinute)+1);
}
if (Secondleft < 0) {
Secondleft = 60 + Secondleft;
Minuteleft = (parseInt(Minuteleft)-1);
}
//分钟
if (NowMinute >60) {
NowMinute =  NowMinute - 60;
NowHour = (parseInt(NowHour)+1);
}
if (Minuteleft < 0) {
Minuteleft = 60 + Minuteleft;
Hourleft = (parseInt(Hourleft)-1);
}
//小时
if (NowHour >24) {
NowHour =  NowHour - 24;
NowDate = (parseInt(NowDate)+1);
}
if (Hourleft < 0) {
Hourleft = 24 + Hourleft;
Dateleft = (parseInt(Dateleft)-1);
}
//天
if (NowDate > 31) {
NowDate = NowDate - 31;
NowMonth = (parseInt(NowMonth)+1);
}
if (Dateleft < 0) {
Dateleft = 31 + Dateleft;
Monthleft = (parseInt(Monthleft)-1);
}
//月
if (NowMonth >12 ) {
NowMonth =  NowMonth - 12;
NowYear = (parseInt(NowYear)+1);
}
if (Monthleft < 0) {
Monthleft = 12 + Monthleft;
Yearleft = (parseInt(Yearleft)-1);
}

Hourleft = Dateleft * 24 + Hourleft ;
if(Hourleft<10){
Hourleft = "0"+Hourleft;
}
if(Minuteleft<10){
Minuteleft = "0"+Minuteleft;
}
if(Secondleft<10){
Secondleft = "0"+Secondleft;
}
//Temp = Hourleft + ':'  + Minuteleft + ':' + Secondleft ;
var status = "${msActivity.status}";
if(status == 1||status == 2){
    if(Hourleft<100){
     Hourleft = '0'+Hourleft;
   
                     $("#timeup").html(Hourleft);
                    
                     }    
$("#timeup").html(Hourleft);
$("#timeup1").html(Minuteleft);
$("#timeup2").html(Secondleft);
if(Hourleft==0&&Minuteleft==0&&Secondleft==0){
  window.location.reload();
}

}else{
$("#timeup").html("000");
$("#timeup1").html("00");
$("#timeup2").html("00");
}

setTimeout("showtime()", 1000);
//timerRunning = true;
}

//var timerID = null;
//var timerRunning = false;
//function stopclock() {
// if (timerRunning)
// clearTimeout(timerID);
// timerRunning = false;
//}
function startclock() {
//stopclock();
showtime(); //调用计算时间差方法,并显示
}
//以上用于计算时间差
//=============================================
function splitTime(time){
year=time.substr(0,4);
var index1=time.indexOf("-");
var index2=time.lastIndexOf("-");
var cha=parseInt(index2)-(parseInt(index1)+1);
month=time.substr((parseInt(index1)+1),cha);
var kg=time.indexOf(" ");
cha=parseInt(kg)-parseInt(index2);
day=time.substr(parseInt(index2)+1,cha);
var mh=time.indexOf(":");
cha=parseInt(mh)-(parseInt(kg)+1);
hour=time.substr(parseInt(kg)+1,cha);
var mh2=time.lastIndexOf(":");
cha=parseInt(mh2)-(parseInt(mh)+1);
minute=time.substr(parseInt(mh)+1,cha);
var mh2=time.lastIndexOf(":");
seconds=time.substr(parseInt(mh2)+1,2);
}

你可能感兴趣的:(html,jsp)