将以下代码拷贝到aspx页面,即可看到效果
<%
// 要比较的数据库中的时间
DateTime dt = DateTime.Parse( " 2008-01-08 15:26:35 " );
long ms = ( long )dt.Subtract(DateTime.Now).TotalSeconds;
%>
< script type = " text/javascript " >
var allSeconds =<%= ms %> ;
function count(){
if (allSeconds <= 0 ){
document.getElementById( " pai " ).disabled = " disabled " ;
}
setTimeout( " count() " , 1000 );
document.getElementById( " lefttime " ).innerHTML = getLeftTimeString(allSeconds);
allSeconds -- ;
}
function getLeftTimeString(ms){
if (ms <= 0 ) return " 竞拍已经结束 " ;
var rr = " 剩余 " ;
if (ms >= 86400 ){
rr = rr + String(Math.floor(ms / 86400 )) + " 天 " ;
ms = ms % 86400 ;
}
if (ms >= 3600 ){
rr = rr + String(Math.floor(ms / 3600 )) + " 小时 " ;
ms = ms % 3600 ;
}
if (ms >= 60 ){
rr = rr + String(Math.floor(ms / 60 )) + " 分钟 " ;
ms = ms % 60 ;
}
rr = rr + String(ms) + " 秒 " ;
return rr;
}
</ script >
< div >
< input id = " pai " type = " button " value = " 竞拍 " />
< span id = " lefttime " ></ span >
< script type = " text/javascript " >
count();
</ script >
</ div >
// 要比较的数据库中的时间
DateTime dt = DateTime.Parse( " 2008-01-08 15:26:35 " );
long ms = ( long )dt.Subtract(DateTime.Now).TotalSeconds;
%>
< script type = " text/javascript " >
var allSeconds =<%= ms %> ;
function count(){
if (allSeconds <= 0 ){
document.getElementById( " pai " ).disabled = " disabled " ;
}
setTimeout( " count() " , 1000 );
document.getElementById( " lefttime " ).innerHTML = getLeftTimeString(allSeconds);
allSeconds -- ;
}
function getLeftTimeString(ms){
if (ms <= 0 ) return " 竞拍已经结束 " ;
var rr = " 剩余 " ;
if (ms >= 86400 ){
rr = rr + String(Math.floor(ms / 86400 )) + " 天 " ;
ms = ms % 86400 ;
}
if (ms >= 3600 ){
rr = rr + String(Math.floor(ms / 3600 )) + " 小时 " ;
ms = ms % 3600 ;
}
if (ms >= 60 ){
rr = rr + String(Math.floor(ms / 60 )) + " 分钟 " ;
ms = ms % 60 ;
}
rr = rr + String(ms) + " 秒 " ;
return rr;
}
</ script >
< div >
< input id = " pai " type = " button " value = " 竞拍 " />
< span id = " lefttime " ></ span >
< script type = " text/javascript " >
count();
</ script >
</ div >
说明:在c#代码中替换对应dt的值,在aspx页面将<%=ms %>输出到js代码内部作为变量,剩下的就交给js了。只是简单模拟,具体逻辑自己再改吧。