最近做了一个倒计时和大家分享一下。
http://s.click.taobao.com/t_8?e=7HZ5x%2BOzdswsVvyc5Jts79Au1Q%3D%3D&p=mm_24156262_0_0
数据库Sql语句
select Datediff(second,getdate(),EndDate)as NumberOfSecond from V_TproductsTAuctionInfo
得到数据的差(NumberOfSecond)
Jquery倒计时
$(document).ready(function() {
Showtime2($("#lblAuctionEndDate").text());
$(".sj[endtime]").each(function(e) {
Showtime($(this).attr('endtime'), $(this).attr('id'));
});
});
function Showtime(vTotalSecond, obj) {
if (vTotalSecond >= 0) {
var vDay = Math.floor(vTotalSecond / 86400);
var vHour = Math.floor(Math.abs(vTotalSecond - vDay * 86400) / 3600);
var vMinute = Math.floor((vTotalSecond - vDay * 86400 - vHour * 3600) / 60);
var vSecond = (vTotalSecond - vDay * 86400 - vHour * 3600 - vMinute * 60);
var vAlert = vDay + "天" + vHour + "小时" + vMinute + "分" + vSecond + "秒";
vTotalSecond = vTotalSecond - 1;
document.getElementById(obj).innerHTML = "<table border='0'><tr><td width='22'> </td><td width='18' class='red'>" + vDay + "</td><td width='12'>天</td>" +
"<td width='20' class='red'>" + vHour + "</td><td width='25'>小时</td><td width='20' class='red'>" + vMinute + "</td><td width='27'>分钟</td><td width='22' class='red'>" +
"" + vSecond + "</td><td width='12'>秒</td></tr></table>";
setTimeout("Showtime(" + vTotalSecond + "," + obj + ")", 1000);
} else {
window.location.reload();
}
}
function Showtime2(vTotalSecond) {
if (vTotalSecond >= 0) {
var vDay = Math.floor(vTotalSecond / 86400);
var vHour = Math.floor(Math.abs(vTotalSecond - vDay * 86400) / 3600);
var vMinute = Math.floor((vTotalSecond - vDay * 86400 - vHour * 3600) / 60);
var vSecond = (vTotalSecond - vDay * 86400 - vHour * 3600 - vMinute * 60);
var vAlert = vDay + "天" + vHour + "小时" + vMinute + "分" + vSecond + "秒";
vTotalSecond = vTotalSecond - 1;
document.getElementById("lblAuctionEndDate").innerHTML = "<table border='0'><tr><td width='22'> </td><td width='18' class='red'>" + vDay + "</td><td width='12'>天</td>" +
"<td width='20' class='red'>" + vHour + "</td><td width='25'>小时</td><td width='20' class='red'>" + vMinute + "</td><td width='27'>分钟</td><td width='22' class='red'>" +
"" + vSecond + "</td><td width='12'>秒</td></tr></table>";
setTimeout("Showtime2(" + vTotalSecond +")", 1000);
} else {
window.location.reload();
}
}
Jquery倒计时,服务器时间计算倒计时
/// <reference path="http://www.cnblogs.com/js/jquery-1.3.2-vsdoc2.js" />
$(document).ready(function() {
var BeginTime = new Date(Date.parse($("#timeField").val().replace(/-/g, "/")));
var EndTime = new Date(Date.parse($("#lblAuctionEndDate").text().replace(/-/g, "/")));
if (BeginTime < EndTime) {
showtime($("#lblAuctionEndDate").text(), "lblAuctionEndDate", $("#timeField").val());
} else {
showtime("0", "lblAuctionEndDate", "0");
}
$(".sj[endtime]").each(function(e) {
var BeginTime2 = new Date(Date.parse($(this).attr('strtime').replace(/-/g, "/")));
var EndTime2 = new Date(Date.parse($(this).attr('endtime').replace(/-/g, "/")));
if (BeginTime2 < EndTime2) {
showtime($(this).attr('endtime'), $(this).attr('id'), $(this).attr('strtime'));
} else {
showtime("0", $(this).attr('id'), "0");
}
}); //倒计时进行绑定
});
function showtime(endtime, obj, strtime) {
if (endtime == strtime) {
window.location.reload();
}
BeginTime = new Date(Date.parse(strtime.replace(/-/g, "/")));
var NowHour = BeginTime.getHours();
var NowMinute = BeginTime.getMinutes();
var NowMonth = BeginTime.getMonth();
var NowDate = BeginTime.getDate();
var NowYear = BeginTime.getYear();
var NowSecond = BeginTime.getSeconds();
if (NowYear < 2000)
NowYear = 1900 + NowYear;
BeginTime.setSeconds(BeginTime.getSeconds() + 1);
strtime = BeginTime.getFullYear() + "-" + (BeginTime.getMonth() + 1) + "-" + BeginTime.getDate() + " " + BeginTime.getHours() + ":" + BeginTime.getMinutes() + ":" + BeginTime.getSeconds();
BeginTime = null;
EndTime = new Date(Date.parse(endtime.replace(/-/g, "/")));
var EndHour = EndTime.getHours();
var EndMinute = EndTime.getMinutes();
var EndMonth = EndTime.getMonth();
var EndDate = EndTime.getDate();
var EndYear = EndTime.getYear();
var EndSecond = EndTime.getSeconds();
if (EndYear < 2000)
EndYear = 1900 + EndYear;
EndTime = null;
Hourleft = EndHour - NowHour
Minuteleft = EndMinute - NowMinute
Secondleft = EndSecond - NowSecond
Yearleft = EndYear - NowYear
Monthleft = EndMonth - NowMonth
Dateleft = EndDate - NowDate
if (Secondleft < 0) {
Secondleft = 60 + Secondleft;
Minuteleft = Minuteleft - 1;
}
if (Minuteleft < 0) {
Minuteleft = 60 + Minuteleft;
Hourleft = Hourleft - 1;
}
if (Hourleft < 0) {
Hourleft = 24 + Hourleft;
Dateleft = Dateleft - 1;
}
if (Dateleft < 0) {
Dateleft = 31 + Dateleft;
Monthleft = Monthleft - 1;
}
if (Monthleft < 0) {
Monthleft = 12 + Monthleft;
Yearleft = Yearleft - 1;
}
Temp = "<table border='0'><tr><td width='22'> </td><td width='18' class='red'>" + (daysBetween(NowYear + "-" + (NowMonth + 1) + "-" + NowDate, EndYear + "-" + (EndMonth + 1) + "-" + EndDate)) + "</td><td width='12'>天</td>" +
"<td width='20' class='red'>" + Hourleft + "</td><td width='25'>小时</td><td width='20' class='red'>" + Minuteleft + "</td><td width='27'>分钟</td><td width='22' class='red'>" +
"" + Secondleft + "</td><td width='12'>秒</td></tr></table>";
document.getElementById(obj).innerHTML = Temp;
setTimeout("showtime('" + endtime + "','" + obj + "','" + strtime + "')", 1000);
}
//+---------------------------------------------------
//| 求两个时间的天数差 日期格式为 YYYY-MM-dd
//+---------------------------------------------------
function daysBetween(DateOne, DateTwo) {
var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));
var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1);
var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));
var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1);
var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000);
return Math.abs(cha);
}
http://s.click.taobao.com/t_8?e=7HZ5x%2BOzdswsVvyc5Jts79Au1Q%3D%3D&p=mm_24156262_0_0