Repeater中使用倒计时

<asp:Label ID="lblTime" runat="server" Text='<%# FormatDateString(Eval("OrderDate").ToString(),(Orders)Container.DataItem) %>'></asp:Label>                                      

JS部分:

/*将列表控件中的"id=lblTime"的控件html转换成页面效果*/
    function clockon() {

        var Elements = document.getElementsByTagName("span");
        for (i = 0; i < Elements.length; i++) {
            var el = Elements[i];
            if (el.id.indexOf('lblTime') >= 0) {

                var id = el.id;
                var source = $("#" + id).html();

                if (source != "") {
                    var arr = new Array();
                    arr = source.split(",");

                    var now = new Date();
                    var year = now.getFullYear(); //getFullYear getYear
                    var month = now.getMonth();
                    var date = now.getDate();
                    var day = now.getDay();
                    var hour = now.getHours();
                    var minu = now.getMinutes();
                    var sec = now.getSeconds();

                    var day1 = new Date(arr[0]); /*到倒计时的日期*/
                    var H1 = parseInt(arr[1]); /*到倒计时的日期指定的几点*/
                    var M1 = parseInt(arr[2]); /*到倒计时的日期几份*/
                    var S1 = parseInt(arr[3]); /*到倒计时的日期几秒*/

                    ms = Math.floor((day1 - now) / 1000) /*到指定的那天0点0分0秒所剩下秒数*/

                    H2 = Math.floor(ms / 3600)
                    M2 = Math.floor((ms - H2 * 3600) / 60)
                    S2 = ms - H2 * 3600 - M2 * 60 + 1

                    S = S2 + S1
                    M = M1 + M2
                    H = H2 + H1

                    if (S > 59) M = M + 1;
                    if (S > 59) S = S - 60;
                    if (M > 59) H = H + 1;
                    if (M > 59) M = M - 60;
                    if (H >= 0) {
                        time2 = "抢购商品剩余支付时间:<br/>" + H + "小时" + M + "" + S + "秒<br/>";
                        $("#" + id).html(time2);
                        //开始倒计时
                        countdown(source, id);
                    }
                    else {
                        $("#" + id).hide();
                        $("#" + id).html("活动已结束");
                    }
                }
                else {
                    $("#" + id).hide();
                    $("#" + id).html("活动已结束");
                }
            }
        }
    }
    //source:到期时间格式(MM/DD/YYYY/,HH,MM,SS);id:控件ID
    function countdown(source, id) {
        var arr = new Array();
        arr = source.split(",");

        var now = new Date();
        var year = now.getFullYear(); //getFullYear getYear
        var month = now.getMonth();
        var date = now.getDate();
        var day = now.getDay();
        var hour = now.getHours();
        var minu = now.getMinutes();
        var sec = now.getSeconds();

        var day1 = new Date(arr[0]); /*到倒计时的日期*/
        var H1 = parseInt(arr[1]); /*到倒计时的日期指定的几点*/
        var M1 = parseInt(arr[2]); /*到倒计时的日期几份*/
        var S1 = parseInt(arr[3]); /*到倒计时的日期几秒*/

        ms = Math.floor((day1 - now) / 1000) /*到指定的那天0点0分0秒所剩下秒数*/

        H2 = Math.floor(ms / 3600)
        M2 = Math.floor((ms - H2 * 3600) / 60)
        S2 = ms - H2 * 3600 - M2 * 60 + 1

        S = S2 + S1
        M = M1 + M2
        H = H2 + H1

        if (S > 59) M = M + 1;
        if (S > 59) S = S - 60;
        if (M > 59) H = H + 1;
        if (M > 59) M = M - 60;
        if (H >= 0) {
            time2 = "抢购商品剩余支付时间:<br/>" + H + "小时" + M + "" + S + "秒<br/>";
            $("#" + id).html(time2);
            var timer = setTimeout("countdown('" + source + "','" + id + "')", 200);
        }
        else {
            $("#" + id).hide();
        }
    }

后台部分:

 需要传入一个结束时间到前台

/// <summary>
    /// 设置前台日期格式显示
    /// </summary>
    /// <param name="string">结束日期时间</param>
    /// <returns></returns>
    public string FormatDateString(string dateString, SecondDie model)
    {
        string result = "";

        if (model.SencondNum > 0)
        {
            string EndDate = DateTime.Parse(dateString).ToString("yyyy-MM-dd HH:mm:ss");

            string year = EndDate.Substring(0, 4);
            string month = EndDate.Substring(5, 2);
            string day = EndDate.Substring(8, 2);

            string hour = EndDate.Substring(11, 2);
            string minute = EndDate.Substring(14, 2);
            string second = EndDate.Substring(17, 2);

            result = month + "/" + day + "/" + year + "," + hour + "," + minute + "," + second;
        }

        return result;
    }

OK! 至此结束

 

posted on 2014-08-05 11:35  绝处而逢生  阅读(214)  评论(0编辑  收藏  举报

导航