如何使用javascript在mvc中显示事件计时器

时间:2016-06-07 05:55:54

标签: javascript asp.net-mvc-4

我正在开发约会时间表申请,我需要使用完整日历的每个约会活动的计时器。 我想显示事件计时器,以便我可以按照计时器启用/禁用呼叫按钮。

Plz帮忙? 提前完成。

1 个答案:

答案 0 :(得分:1)

试试这段代码

设置当前日期

TimeZoneInfo timeZoneInfo =   TimeZoneInfo.FindSystemTimeZoneById("Timezonename");

 var d = new Date('@DateTime.UtcNow.ToTimeZoneTime(timeZoneInfo)');

计算剩余时间

function getTimeRemaining(endtime){
        var t = Date.parse(endtime) - Date.parse(d);
        var seconds = Math.floor( (t/1000) % 60 );
        var minutes = Math.floor( (t/1000/60) % 60 );
        var hours = Math.floor( (t/(1000*60*60)) % 24 );
        var days = Math.floor( t/(1000*60*60*24) );
        return {
            'total': t,
            'days': days,
            'hours': hours,
            'minutes': minutes,
            'seconds': seconds
        };
    }

    var timeinterval;

启动时钟

var timeinterval;
    function initializeClock(endtime){
        var clock = document.getElementById(id);
        timeinterval = setInterval(function(){
            var t = getTimeRemaining(endtime);
            if(t.total<0)
            {
                t.seconds =((-1)*parseInt(t.seconds))-1;
                t.minutes = ((-1)*parseInt(t.minutes))-1;
                t.hours = ((-1)*parseInt(t.hours))-1;
                t.days =  ((-1)*parseInt(t.days))-1;
            }

            t.days= t.days<=9?'0'+t.days:t.days
            t.hours= t.hours<=9?'0'+t.hours:t.hours
            t.minutes= t.minutes<=9?'0'+t.minutes:t.minutes
            t.seconds= t.seconds<=9?'0'+t.seconds:t.seconds


            var text=(t.days>0)?t.days  + ' d ':'';

            if(t.total<=0){

                    text='Started '
                    text+=(t.days>0)?t.days  + ' d ':'';
                    text+=(t.hours>0) || (t.days>0)?t.hours  + ' h ':'';
                    text+=(t.minutes>0) || (t.hours>0)?t.minutes  + ' m ':'';
                    text+=(t.seconds>0) || (t.minutes>0)?t.seconds  + ' s ago ':'';

            }
            else{

                text+=(t.hours>0) || (t.days>0)?t.hours  + ' h ':'';
                text+=(t.minutes>0) || (t.hours>0)?t.minutes  + ' m ':'';
                text+=(t.seconds>0) || (t.minutes>0)?t.seconds  + ' s left ':'';
            }
            clock.text(text);
        },1000);
    }