js秒毫秒倒计时

后台:

protected void Page_Load(object sender, EventArgs e)
    {
        int timediff=DateDiff(DateTime.Parse("2011/3/5 18:00:00"),System.DateTime.Now);
        ClientScript.RegisterStartupScript(this.GetType(), "time", "<script>var duration=" + timediff + ";var endTime = new Date().getTime() + duration + 100;</script>");
    }
    private int DateDiff(DateTime DateTime1, DateTime DateTime2)
   {
         int dateDiff = 0;
         TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
         TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
         TimeSpan ts = ts1.Subtract(ts2).Duration();
         dateDiff = (ts.Days * 24 * 60 * 60 + ts.Hours * 60 * 60 + ts.Minutes * 60 + ts.Seconds)*1000;//获取毫秒数
         return dateDiff;
   }

前台:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <script type="text/javascript">
     <!--               
     function interval()
     {
         var n=(endTime-new Date().getTime())/1000;
         if(n<0) return;
         document.getElementById("timeout").innerHTML = n.toFixed(3);
         setTimeout(interval, 10);
     }
     window.onload=function()
     {
         interval();
     }
     //-->
 </script>
</head>
<body >

<form id="form1" runat="server">

<div id="timeout"s></div>
</form>
</div>
</body>
</html>


你可能感兴趣的:(js秒毫秒倒计时)