C#时间倒计时代码讲解

发布于: 2018-09-27    浏览: 128    作者:Wu

做时间倒计时不习惯用Timer,可以用TimeSpan做比较进行倒计时

后台:


        TimeSpan ts = 获取的时间.AddDays(1) - DateTime.Now;
           intt = Convert.ToInt32((ts.TotalSeconds - ts.TotalSeconds %1).ToString());
           if(t <0)
           {
              lab.Value= DateTime.Now.ToString();
           }
           else
           {
               lab.Value = DateTime.Now.AddSeconds(t).ToString();

           }


注意:获取时间后,通过倒计时的时间减去当前时间进行的判断,也可有其他方式,这只是其中的一种

一般倒计时一天,就可通过 “AddDays(1)”进行相加,还有AddHours等

前端:



        <asp:Label ID="labtime"runat="server"></asp:Label>    
 
                $(function () {
                        setInterval(function () {
                            var times = $("获取的后台传来的值").val();
                            if(times !="") {
                                var s = compute(times);
                            }
                        },1000);
 
                        function compute() {
                            var t =newDate(arguments[0]).getTime() /1000, n =                                 Math.round(newDate().getTime() /1000), x = t > n ? t - n :0;
                            if(x >0) {
                                var s = x %60, m = parseInt((x % (3600)) / (60)), h = parseInt((x% (86400)) / (3600)), d = parseInt(x / (86400));
                                if(parseInt(d) >0|| parseInt(h) >0) {
                                    document.getElementById('labtime').innerHTML ="0时0分0秒";
                                }
                                document.getElementById('labtime').innerHTML = h +"时"+ m +"分"+ s +"秒";
                            }else{
                                document.getElementById('labtime').innerHTML ="0时0分0秒";
                            }
                        };

                    });


 ——本文并非原创,如有侵权请联系管理员删除。

在线客服

售前咨询

售后服务

投诉/建议

服务热线
0731-82880004
18874148081