[转载]c# 获取倒计时剩余天数(c#计算两个日期相差的天数)

.

C#代码:

/// <summary>
    /// 获取倒计时剩余天数
    /// </summary>
    /// <returns></returns>
    private int leftdays(DateTime endDate)
    {
        DateTime today = DateTime.Now;  //当前日期
        DateTime testday = endDate; //结束日期

        TimeSpan tdays = testday - today;  //得到时间差

        //int ld = tdays.Days + 1;//得到剩余天数 这里是否加1 根据实际情况来定
        int seconds = tdays.Seconds + 86400;//得到剩余描述 这里是否加1 根据实际情况来定

        return seconds ;
    }

页面js代码:

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>index</title>
<style type="text/css">
em{color:#f00;}
</style>
</head>

<body>
<div id="remaintime"></div>
<script type="text/javascript">

    var TheTime = function() {
        this.init.apply(this, arguments);
    };

    TheTime.prototype = {
        init: function(obj) {
            var that = this;
            obj = that.buildParam(obj);
            that.callback = obj.callback;
            var container = that.container = document.getElementById(obj.container);
            container.innerHTML = '<span class="red2"><em></em>天<em></em>小时<em></em>分钟<em></em>秒</span>';
            
            var daySpace=that.daySpace=container.getElementsByTagName('em')[0];
            var hourSpace = that.hourSpace = container.getElementsByTagName('em')[1];
            var minuteSpace = that.minuteSpace = container.getElementsByTagName('em')[2];
            var secondSpace = that.secondSpace = container.getElementsByTagName('em')[3];

            if (obj.remaintime == 0) {
                that.resetTime();
                return;
            }
        
            that.hours = Math.floor(obj.remaintime / 3600);
            that.days=Math.floor(that.hours/24);
            that.hours=that.hours-that.days*24;
            that._remainder1 = obj.remaintime % 3600;
            that.minutes = Math.floor(that._remainder1 / 60);
            that.seconds = that._remainder1 % 60;
            var timer = that.timer = setInterval(function() {
                that.renderTime.apply(that);
            }, 1000);
        },
        buildParam: function(obj) {
            obj = {
                //container为dom节点的id
                container: obj.container || 'container',
                remaintime: Number(obj.remaintime) || 0,
                //倒计时完成后的回调
                callback: obj.callback || new Function
            };
            return obj;
        },
        resetTime: function() {
            var that = this;
            that.container.innerHTML = "已经截止";
        },
        //刷新时间
        renderTime: function() {
            //debugger;
            var that = this;
            if (that.seconds > 0) {
                that.seconds--;
            } else {
                that.seconds = 59;
                if (that.minutes > 0) {
                    that.minutes--;
                } else {
                    that.minutes = 59;
                    if (that.hours > 0) {
                        that.hours--;
                    }else
                    {
                        that.hours=24;
                        if(that.days>0)
                        {
                            that.days--;
                        }
                    }
                }
            }

            //时刻监听
            if (that.hours == 0 && that.minutes == 0 && that.seconds == 0) {
                //执行回调
                that._callback();
            }
            var bitHandle = that.bitHandle;

            var _day=bitHandle(that.days);
            var _hour = bitHandle(that.hours);
            var _minute = bitHandle(that.minutes);
            var _second = bitHandle(that.seconds);
            that.hourSpace.innerHTML = _hour;
            that.minuteSpace.innerHTML = _minute;
            that.secondSpace.innerHTML = _second;
            that.daySpace.innerHTML=_day;
        },
        //对于位数的处理,确保返回两位数
        bitHandle: function(num) {
            var str = num.toString();
            if (str.length < 2) {
                str = 0 + str;
            }
            return str;
        },
        _callback: function() {
            var that = this;
            clearInterval(that.timer);
            that.callback();
        }

    };

    new TheTime({
        //容器id
        container: 'remaintime',
        //服务器返回的剩余时间,单位为秒
        remaintime:<%=second %>,
        //倒计时完成时的回调
        callback: function() {
            document.getElementById('remaintime').innerHTML = '已截止';
        }
    });
</script>
</body>
</html>

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏