日期:2014-05-16  浏览次数:20326 次

秒杀倒计时实现 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<1