asp.net问题,一个计算时间差的问题,怎么能让时间到计时?下面代码总是停留在1小时 20
DateTimedt11=DateTime.Now;DateTimedt22=DateTime.Now;if(Label2.Text==""){dt22=dt11.Add...
DateTime dt11 = DateTime.Now;
DateTime dt22 = DateTime.Now;
if (Label2.Text=="")
{
dt22 = dt11.AddHours(-1);
Label2.Text = dt11.ToString();
}
if (Label2.Text!="")
{
TimeSpan dtt = dt11 - dt22;
Label2.Text = "您的菜还有"+dtt.Hours.ToString() + "时" + dtt.Minutes.ToString() + "分" + dtt.Seconds.ToString() + "秒收获";
} 展开
DateTime dt22 = DateTime.Now;
if (Label2.Text=="")
{
dt22 = dt11.AddHours(-1);
Label2.Text = dt11.ToString();
}
if (Label2.Text!="")
{
TimeSpan dtt = dt11 - dt22;
Label2.Text = "您的菜还有"+dtt.Hours.ToString() + "时" + dtt.Minutes.ToString() + "分" + dtt.Seconds.ToString() + "秒收获";
} 展开
展开全部
你这个问题要用JS完成,一秒一秒地变化不可能老是向服务器发送请求的,不回发,你的值不会变化的。
这段代码是每秒都变化的。
<form name="form2">
<div align="center" align="center">
<center>离2012年还有:<br>
<input type="textarea" name="left" size="35" style="text-align: center"/>
</center>
</div>
<script LANGUAGE="javascript">
startclock();
var timerID = null;
var timerRunning = false;
function showtime() {
var Today = new Date();
var NowHour = Today.getHours();
var NowMinute = Today.getMinutes();
var NowMonth = Today.getMonth();
var NowDate = Today.getDate();
var NowYear = Today.getYear();
var NowSecond = Today.getSeconds();
if (NowYear <2000)
NowYear=1900+NowYear;
Today = null;
Hourleft = 23 - NowHour
Minuteleft = 59 - NowMinute
Secondleft = 59 - NowSecond
Yearleft = 2012 - NowYear
Monthleft = 12 - NowMonth - 1
Dateleft = 31 - 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=Yearleft+'年, '+Monthleft+'月, '+Dateleft+'天, '+Hourleft+'小时, '+Minuteleft+'分, '+Secondleft+'秒'
document.form2.left.value=Temp;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
var timerID = null;
var timerRunning = false;
function stopclock () {
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function startclock () {
stopclock();
showtime();
}
// -->
</script>
</form>
下面是我帮你改了个简单的:
<SCRIPT LANGUAGE="JavaScript">
<!--
var maxtime = 5*60*60 //五个小时,要几个小时就把前面的5改成几
function CountDown(){
if(maxtime>=0){
minutes = Math.floor((maxtime/60)%60);
seconds = Math.floor(maxtime%60);
hours=Math.floor(maxtime/3600);
msg = "距离结束还有"+hours+"小时"+minutes+"分"+seconds+"秒";
document.getElementById("timer").innerHTML=msg;
if(maxtime == 5*60) alert('注意,还有5分钟!');
--maxtime;
}
else{
clearInterval(timer);
alert("时间到,结束!");
}
}
timer = setInterval("CountDown()",10);
//-->
</SCRIPT>
<div id="timer" style="color:red; padding-left:300px;"></div>
这段代码是每秒都变化的。
<form name="form2">
<div align="center" align="center">
<center>离2012年还有:<br>
<input type="textarea" name="left" size="35" style="text-align: center"/>
</center>
</div>
<script LANGUAGE="javascript">
startclock();
var timerID = null;
var timerRunning = false;
function showtime() {
var Today = new Date();
var NowHour = Today.getHours();
var NowMinute = Today.getMinutes();
var NowMonth = Today.getMonth();
var NowDate = Today.getDate();
var NowYear = Today.getYear();
var NowSecond = Today.getSeconds();
if (NowYear <2000)
NowYear=1900+NowYear;
Today = null;
Hourleft = 23 - NowHour
Minuteleft = 59 - NowMinute
Secondleft = 59 - NowSecond
Yearleft = 2012 - NowYear
Monthleft = 12 - NowMonth - 1
Dateleft = 31 - 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=Yearleft+'年, '+Monthleft+'月, '+Dateleft+'天, '+Hourleft+'小时, '+Minuteleft+'分, '+Secondleft+'秒'
document.form2.left.value=Temp;
timerID = setTimeout("showtime()",1000);
timerRunning = true;
}
var timerID = null;
var timerRunning = false;
function stopclock () {
if(timerRunning)
clearTimeout(timerID);
timerRunning = false;
}
function startclock () {
stopclock();
showtime();
}
// -->
</script>
</form>
下面是我帮你改了个简单的:
<SCRIPT LANGUAGE="JavaScript">
<!--
var maxtime = 5*60*60 //五个小时,要几个小时就把前面的5改成几
function CountDown(){
if(maxtime>=0){
minutes = Math.floor((maxtime/60)%60);
seconds = Math.floor(maxtime%60);
hours=Math.floor(maxtime/3600);
msg = "距离结束还有"+hours+"小时"+minutes+"分"+seconds+"秒";
document.getElementById("timer").innerHTML=msg;
if(maxtime == 5*60) alert('注意,还有5分钟!');
--maxtime;
}
else{
clearInterval(timer);
alert("时间到,结束!");
}
}
timer = setInterval("CountDown()",10);
//-->
</SCRIPT>
<div id="timer" style="color:red; padding-left:300px;"></div>
展开全部
HTML代码:
<script language="JavaScript" type="text/javascript">
function djs(){
var urodz= new Date("11/12/2008");
var now = new Date();
var num
var ile = urodz.getTime() - now.getTime();
var dni = Math.floor(ile / (1000 * 60 * 60 * 24));
if (dni >1)
num=dni+1
else if (dni == 1) num=2
else if (dni == 0) num=1
else num=0
document.write(num)
}
</script>
距某某开幕式还有 [<script language="JavaScript" type="text/javascript">djs()</script>] 天
<script language="JavaScript" type="text/javascript">
function djs(){
var urodz= new Date("11/12/2008");
var now = new Date();
var num
var ile = urodz.getTime() - now.getTime();
var dni = Math.floor(ile / (1000 * 60 * 60 * 24));
if (dni >1)
num=dni+1
else if (dni == 1) num=2
else if (dni == 0) num=1
else num=0
document.write(num)
}
</script>
距某某开幕式还有 [<script language="JavaScript" type="text/javascript">djs()</script>] 天
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
问题比较多,要倒计时你应该先固定一个时间,比如说到2011/10/21 12:00
DateTime dt11 = DateTime.Now;
DateTime dt22 = DateTime.Parse("2011/10/21 12:00:00");
TimeSpan dtt = dt11 - dt22;
Label2.Text = "您的菜还有"+dtt.Hours.ToString() + "时" + dtt.Minutes.ToString() + "分" + dtt.Seconds.ToString() + "秒收获";
DateTime dt11 = DateTime.Now;
DateTime dt22 = DateTime.Parse("2011/10/21 12:00:00");
TimeSpan dtt = dt11 - dt22;
Label2.Text = "您的菜还有"+dtt.Hours.ToString() + "时" + dtt.Minutes.ToString() + "分" + dtt.Seconds.ToString() + "秒收获";
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询