計算時間差的代碼


ElapsedTime =
{
        threshold :
        {
                zero : 0,
                secondsPerMinute : 60,
                secondsPerHour : 60 * 60,
                secondsPerDay : 60 * 60 * 24,
                secondsPerMonth : 60 * 60 * 24 * 30,
                secondsPerYear : 60 * 60 * 24 * 30 * 365
        },

        suffix :
        {
                negative : '請檢查您的時間設置',
                second : '分鐘前',
                minute : '分鐘前',
                hour : '小時前',
                day : '天前',
                month : '月前',
                year : '年前'
        },

        now : new Date(),
        caculate : function(date)
        {
                var secondsDistance = (this.now - date) / 1000;
                if(secondsDistance < this.threshold.zero)
                {
                        return this.suffix.negative;
                }
                else if(secondsDistance < this.threshold.secondsPerMinute)
                {
                        return '1' + promat.second;
                        //按照要求特殊處理
                }
                else if(secondsDistance < this.threshold.secondsPerHour)
                {
                        return parseInt(secondsDistance / this.threshold.secondsPerMinute) + this.suffix.minute;
                }
                else if(secondsDistance < this.threshold.secondsPerDay)
                {
                        return parseInt(secondsDistance / this.threshold.secondsPerHour) + this.suffix.hour;
                }
                else if(secondsDistance < this.threshold.secondsPerMonth)
                {
                        return parseInt(secondsDistance / this.threshold.secondsPerDay) + this.suffix.day;
                }
                else if(secondsDistance < this.threshold.secondsPerYear)
                {
                        return parseInt(secondsDistance / this.threshold.secondsPerMonth) + this.suffix.month;
                }
                else
                {
                        return parseInt(secondsDistance / this.threshold.secondsPerMinute) + this.suffix.year;
                }

        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章