技术栈

主页 > 前端开发 >

一个时间的小工具_更新倒计时功能

时间小插件

其中有类型判断,模块扩展数组化的综合应用 ,如果可以翻墙可以来https://github.com/zasqw2222/time

(function(w) {
// 原型
var Time = function() {
    if (!(this instanceof Time)) {
        return new Time();
    }
    this.time = new Date();
};

// getFullTime 对象
var fullTime = {
    getFullTime: function() {
        var _time = this.time;
        return {
            day: _time.getDate(),
            week: _time.getDay(),
            month: _time.getMonth() + 1,
            year: _time.getFullYear(),
            hours: _time.getHours(),
            minute: _time.getMinutes(),
            seconds: _time.getSeconds(),
            mseconds: _time.getMilliseconds(),
            hab: _time.getFullYear() + '年' + (_time.getMonth() + 1) + '月' + _time.getDate() + '日 星期' + toCweek(_time.getDay()) + ' ' + _time.getHours() + '时' + _time.getMinutes() + '分' + _time.getSeconds() + '秒'
        }
    },
    isLeapYear: function(ele) { //是否是润年
        var ele = ele || this.time.getFullYear(),
            // temp = new Date(ele + '/' + 3 + '/0').getDate();
            temp = new Date(ele,2,0).getDate();
            // console.log(temp+'..')
        return (temp === 29) ? true : false;
    },
    quantityInMonth: function(year, month) { //查询月内有多少天
        var year = year || this.time.getFullYear(),
            month = month || (this.time.getMonth() + 1);

        // return new Date(year + '/' + (month + 1) + '/0').getDate();
        return new Date(year,(month + 1),0).getDate();
    },
    //2010-10-12 01:00:00 这是时间格式
    timeDiff: function(startTime, endTime, diffType) { //相距时间
        startTime = startTime.replace(/-/g, "/");
        endTime = endTime.replace(/-/g, "/");
        diffType = diffType.toLowerCase();
        var sTime = new Date(startTime), //开始时间
            eTime = new Date(endTime), //结束时间
            divNum = 1;
        switch (diffType) {
            case "second":
                divNum = 1000;
                break;
            case "minute":
                divNum = 1000 * 60;
                break;
            case "hour":
                divNum = 1000 * 3600;
                break;
            case "day":
                divNum = 1000 * 3600 * 24;
                break;
            default:
                break;
        }
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
    }
}


// 深度复制工具
function extendDeep() {
    var i,
        target = arguments[0] || {},
        astr = '[object Array]',
        toStr = Object.prototype.toString,
        yArr = Array.prototype.slice.call(arguments, 1);
    for (i = 0, len = yArr.length; i < len; i++) {
        var temp = yArr[i];
        for (var j in temp) {
            if (target.hasOwnProperty(j) && (target[i] === temp[i])) {
                continue;
            }
            if (temp.hasOwnProperty(j)) {
                if (typeof temp[j] === 'object') {
                    target[j] = (toStr.call(temp[j] === astr)) ? [] : {};
                    extendDeep(target[j], temp[j]);
                } else {
                    if (typeof temp[j] !== 'undefined') {
                        target[j] = temp[j];
                    }
                }
            }
        }
    }
    return target;
}


// 工具函数区

/**
 * [getType 获取任意元素类型]
 * @param  {[none]} ele [要判断类型的元素]
 * @return {[string]}     [返回字符串型类型描述]
 */
function getType(ele) {
    var oStr = Object.prototype.toString.call(ele),
        reg = /[object (.*)]/,
        arr = reg.exec(oStr);
    return arr[1];
}

/**
 * [isArray 判断是不是数组]
 * @param  {[元素]}  ele [要判断类型的变量]
 * @return {Boolean}     [如果是数组就返回true,反之false]
 */
function isArray(ele) {
    return (getType(ele) === 'Array') ? true : false;
}

/**
 * [isObject 判断是不是对象]
 * @param  {[元素]}  ele [要判断类型的变量]
 * @return {Boolean}     [如果是对象就返回true,反之false]
 */
function isObject(ele) {
        return (getType(ele) === 'Object') ? true : false;
}
/**
 * [toCweek 把数字转换成汉字的周几]
 * @param  {[number]} ele [数字的周几]
 * @return {[string]}     [汉字的周几]
 */
function toCweek(ele) {
        if (getType(ele) !== 'Number') {
            ele = Number(ele)
            if (isNaN(ele)) {
                return undefined;
            }
        }
        switch (ele) {
            case 0:
                return '日';
                break;
            case 1:
                return '一';
                break;
            case 2:
                return '二';
                break;
            case 3:
                return '三';
                break;
            case 4:
                return '四';
                break;
            case 5:
                return '五';
                break;
            case 6:
                return '六';
                break;
        }
    }
    // 扩展方法getFullTime
extendDeep(Time.prototype, fullTime);
extendDeep(Time.prototype, {
    version: '1.0'
})
if (typeof w.Time === 'undefined') {
    w.Time = Time;
}
})(window)

更新版本_优化代码 优化代码带来来自 yanhaijing 感谢

;(function(w) {
    // 深度复制工具
    function extendDeep() {
        var i,
            target = arguments[0] || {},
            astr = '[object Array]',
            toStr = Object.prototype.toString,
            yArr = Array.prototype.slice.call(arguments, 1);
        for (i = 0, len = yArr.length; i < len; i++) {
            var temp = yArr[i];
            for (var j in temp) {
                if (target.hasOwnProperty(j) && (target[i] === temp[i])) {
                    continue;
                }
                if (temp.hasOwnProperty(j)) {
                    if (typeof temp[j] === 'object') {
                        target[j] = (toStr.call(temp[j] === astr)) ? [] : {};
                        extendDeep(target[j], temp[j]);
                    } else {
                        if (typeof temp[j] !== 'undefined') {
                            target[j] = temp[j];
                        }
                    }
                }
            }
        }
        return target;
    }


    // 工具函数区

    /**
     * [getType 获取任意元素类型]
     * @param  {[none]} ele [要判断类型的元素]
     * @return {[string]}     [返回字符串型类型描述]
     */
    function getType(ele) {
        var oStr = Object.prototype.toString.call(ele),
            reg = /[object (.*)]/,
            arr = reg.exec(oStr);
        return arr[1];
    }

    /**
     * [isArray 判断是不是数组]
     * @param  {[元素]}  ele [要判断类型的变量]
     * @return {Boolean}     [如果是数组就返回true,反之false]
     */
    function isArray(ele) {
        return (getType(ele) === 'Array') ? true : false;
    }

    /**
     * [isObject 判断是不是对象]
     * @param  {[元素]}  ele [要判断类型的变量]
     * @return {Boolean}     [如果是对象就返回true,反之false]
     */
    function isObject(ele) {
        return (getType(ele) === 'Object') ? true : false;
    }

    /**
     * [toCweek 把数字转换成汉字的周几]
     * @param  {[number]} ele [数字的周几]
     * @return {[string]}     [汉字的周几]
     */
    function toCweek(ele) {
        if (getType(ele) !== 'Number') {
            ele = Number(ele);
            if (isNaN(ele)) {
                return undefined;
            }
        }

        ele = ele % 7;//大于七的处理

        return weeks[ele];
    }

    // 原型
    var Time = function() {
        if (!(this instanceof Time)) {
            return new Time();
        }
        this.time = new Date();
    };
    var diffTypes = {
        second: 1000,
        minute: 60000,
        hour: 3600000,
        day: 86400000
    };
    var weeks = ['日','一','二','三','四','五','六'];
    // getFullTime 对象
    var fullTime = {
        getFullTime: function() {
            var _time = this.time;
            return {
                day: _time.getDate(),
                week: _time.getDay(),
                month: _time.getMonth() + 1,
                year: _time.getFullYear(),
                hours: _time.getHours(),
                minute: _time.getMinutes(),
                seconds: _time.getSeconds(),
                mseconds: _time.getMilliseconds(),
                hab: _time.getFullYear() + '年' + (_time.getMonth() + 1) + '月' + _time.getDate() + '日 星期' + toCweek(_time.getDay()) + ' ' + _time.getHours() + '时' + _time.getMinutes() + '分' + _time.getSeconds() + '秒'
            };
        },
        isLeapYear: function(ele) { //是否是润年
            ele = ele || this.time.getFullYear();
            // temp = new Date(ele + '/' + 3 + '/0').getDate();
            var temp = new Date(ele,2,0).getDate();
            // console.log(temp+'..')
            return (temp === 29) ? true : false;
        },
        quantityInMonth: function(year, month) { //查询月内有多少天
            year = year || this.time.getFullYear();
            month = month || (this.time.getMonth() + 1);

            // return new Date(year + '/' + (month + 1) + '/0').getDate();
            return new Date(year,(month + 1),0).getDate();
        },
        //2010-10-12 01:00:00 这是时间格式
        timeDiff: function(startTime, endTime, diffType) { //相距时间
            startTime = startTime.replace(/-/g, "/");
            endTime = endTime.replace(/-/g, "/");
            diffType = diffType.toLowerCase();
            var sTime = new Date(startTime), //开始时间
                eTime = new Date(endTime), //结束时间
                divNum = diffTypes[diffType] || 1;

            return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum, 10), 10);
        }
    };

    // 扩展方法getFullTime
    extendDeep(Time.prototype, fullTime);
    extendDeep(Time.prototype, {
        version: '1.0'
    });
    if (typeof w.Time === 'undefined') {
        w.Time = Time;
    }

})(window);

更新20141223静态方法更新

;(function(w) {
    // 深度复制工具
    function extendDeep() {
        var i,
            target = arguments[0] || {},
            astr = '[object Array]',
            toStr = Object.prototype.toString,
            yArr = Array.prototype.slice.call(arguments, 1);
        for (i = 0, len = yArr.length; i < len; i++) {
            var temp = yArr[i];
            for (var j in temp) {
                if (target.hasOwnProperty(j) && (target[i] === temp[i])) {
                    continue;
                }
                if (temp.hasOwnProperty(j)) {
                    if (typeof temp[j] === 'object') {
                        target[j] = (toStr.call(temp[j] === astr)) ? [] : {};
                        extendDeep(target[j], temp[j]);
                    } else {
                        if (typeof temp[j] !== 'undefined') {
                            target[j] = temp[j];
                        }
                    }
                }
            }
        }
        return target;
    }
    // 工具函数区
    var tool_type = {
        getType : function(ele){
            var oStr = Object.prototype.toString.call(ele),
            reg = /[object (.*)]/,
            arr = reg.exec(oStr);
            return arr[1];
        },
        isArray : function (ele) {
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isObject : function (ele) {
            return (this.getType(ele) === 'Object') ? true : false;
        }
    }
    /**
     * [toCweek 数字转换汉字]
     * @param  {[number]} ele [传入数字]
     * @return {[string]}     [返回汉字]
     */
    function toCweek(ele) {
        if (tool_type.getType(ele) !== 'Number') {
            ele = Number(ele);
            if (isNaN(ele)) {
                return undefined;
            }
        }
        ele = ele % 7;//大于七的处理
        return weeks[ele];
    }
    //2014,12,10 || '2014,12,10'
    /**
     * [paramToArray 根据参数判断返回何等时间]
     * @param  {[type]} args [arguments]
     * @return {[Date]}      [根据不同的设定时间返回时间对象]
     */
    function paramToArray(args){
        args = args[0];
        var arr={};
        if(args.length === 0){
            return new Date();
        }
        if(tool_type.getType(args[0]) === 'String'){
            var str = args[0],
            arr1 = str.split(',');
            arr = {
                yyyy : arr1[0] || '',
                mth : arr1[1] || '',
                dd : arr1[2] || '',
                hh : arr1[3] || '',
                min : arr1[4] || '',
                ss : arr1[5] || '',
                sss : arr1[6] || '',
            }
            return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss,arr.sss)
        }
         arr = {
            yyyy : args[0] || '',
            mth : args[1] || '',
            dd : args[2] || '',
            hh : args[3] || '',
            min : args[4] || '',
            ss : args[5] || '',
            sss : args[6] || ''
        }
        return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss,arr.sss);
    }

    // 原型
    var Time = function() {
        if (!(this instanceof Time)) {
            return new Time(arguments);
        }
        this.time = paramToArray(arguments);
    };
    //时间差基础单位
    var diffTypes = {
        second: 1000,
        minute: 60000,
        hour: 3600000,
        day: 86400000
    };
    //周单位
    var weeks = ['日','一','二','三','四','五','六'];

    /**
     * [isLeapYear 是否闰年]
     * @param  {[type]}  ele [description]
     * @return {Boolean}     [description]
     */
    function isLeapYear(ele){
        ele = ele || this.time.getFullYear();
        var temp = new Date(ele,2,0).getDate();
        return (temp === 29) ? true : false;
    };
    /**
     * [quantityInMonth 给定月中返回有多少天]
     * @param  {[type]} year  [description]
     * @param  {[type]} month [description]
     * @return {[type]}       [description]
     */
    function quantityInMonth(year,month){
        year = year || this.time.getFullYear();
        month = month || this.time.getMonth();
        console.log(year+'...'+month)
        return new Date(year,month,0).getDate();
    };
    /**
     * [timeDiff 2个时间差]
     * @param  {[type]} startTime [description]
     * @param  {[type]} endTime   [description]
     * @param  {[type]} diffType  [description]
     * @return {[type]}           [description]
     */
    function timeDiff(startTime, endTime, diffType){
        startTime = startTime.replace(/-/g, "/");
        endTime = endTime.replace(/-/g, "/");
        diffType = diffType.toLowerCase();
        var sTime = new Date(startTime), //开始时间
            eTime = new Date(endTime), //结束时间
            divNum = diffTypes[diffType] || 1;
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum, 10), 10);
    }
    /**
     * [getFullTime 格式化时间]
     * @param  {[b]} b [是否使用当前时间]
     * @return {[type]}   [description]
     */
    function getFullTime(b){
        var _time = b ? new Date() : this.time;
        return {
            day: _time.getDate(),
            week: _time.getDay(),
            month: _time.getMonth() + 1,
            year: _time.getFullYear(),
            hours: _time.getHours(),
            minute: _time.getMinutes(),
            seconds: _time.getSeconds(),
            mseconds: _time.getMilliseconds(),
            hab: _time.getFullYear() + '年' + (_time.getMonth() + 1) + '月' + _time.getDate() + '日 星期' + toCweek(_time.getDay()) + ' ' + _time.getHours() + '时' + _time.getMinutes() + '分' + _time.getSeconds() + '秒'
        }
    };
    // 扩展
    extendDeep(Time.prototype, {
        version: '1.0',
        getFullTime : getFullTime,
        timeDiff : timeDiff,
        quantityInMonth :quantityInMonth,
        isLeapYear : isLeapYear
    });
    if (typeof w.Time === 'undefined') {
        w.Time = Time;
    }

})(window);

更新倒计时功能

;(function(w) {
    // 深度复制工具
    function extendDeep() {
        var i,
            target = arguments[0] || {},
            astr = '[object Array]',
            toStr = Object.prototype.toString,
            yArr = Array.prototype.slice.call(arguments, 1);
        for (i = 0, len = yArr.length; i < len; i++) {
            var temp = yArr[i];
            for (var j in temp) {
                if (target.hasOwnProperty(j) && (target[i] === temp[i])) {
                    continue;
                }
                if (temp.hasOwnProperty(j)) {
                    if (typeof temp[j] === 'object') {
                        target[j] = (toStr.call(temp[j] === astr)) ? [] : {};
                        extendDeep(target[j], temp[j]);
                    } else {
                        if (typeof temp[j] !== 'undefined') {
                            target[j] = temp[j];
                        }
                    }
                }
            }
        }
        return target;
    }
    // 工具函数区
    var tool = {
        getType : function(ele){
            var oStr = Object.prototype.toString.call(ele),
            reg = /[object (.*)]/,
            arr = reg.exec(oStr);
            return arr[1];
        },
        isArray : function (ele) {
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isObject : function (ele) {
            return (this.getType(ele) === 'Object') ? true : false;
        }
        // makeArray : function(ele){//有问题
        //   var target = [];
        //   if(ele != null){
        //      var i = ele.length;
        //      console.log(i)
        //      if(i = null || typeof ele === 'string' ||  typeof ele === 'function' || ele.setInterval){
        //          target[0] = ele;
        //          console.log('走这里了?')
        //      }else{
        //          console.log('走这里了1?' + i)
        //          while(i){
        //              console.log(i)
        //              target[--i] = ele[i];
        //          }

        //      }
        //   }
        //   // console.log(target)
        //  return target;
        // }
    }
    /**
     * [$arr 数组话函数]
     * @param  {[元素或类数组]} ele [传进的元素或者类数组话]
     * @return {[type]}     [description]
     */
    function $arr(ele) {
      if (!ele){
        return [];
      } 
      if (ele.toArray){
        return ele.toArray();
      } 
      var length = ele.length || 0, results = new Array(length);

      while (length--){
        results[length] = ele[length];
      } 
      return results;
    }
    /**
     * [toCweek 数字转换汉字]
     * @param  {[number]} ele [传入数字]
     * @return {[string]}     [返回汉字]
     */
    function toCweek(ele) {
        if (tool.getType(ele) !== 'Number') {
            ele = Number(ele);
            if (isNaN(ele)) {
                return undefined;
            }
        }
        ele = ele % 7;//大于七的处理
        return weeks[ele];
    }
    //2014,12,10,10,10,10 || '2014,12,10,10,10,10'
    /**
     * [paramToArray 根据参数判断返回何等时间]
     * @param  {[type]} args [arguments]
     * @return {[Date]}      [根据不同的设定时间返回时间对象]
     */
    function paramToArray(args){//扩展次参数操作
        args = args[0];
        var arr={};
        if(args.length === 0){
            return new Date();
        }
        if(tool.getType(args[0]) === 'String'){
            var str = args[0],
            arr1 = str.split(',');
            arr = {
                yyyy : arr1[0] || '',
                mth : arr1[1] || '',
                dd : arr1[2] || '',
                hh : arr1[3] || '',
                min : arr1[4] || '',
                ss : arr1[5] || ''
            }
            return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss);
        }
         arr = {
            yyyy : args[0] || '',
            mth : args[1] || '',
            dd : args[2] || '',
            hh : args[3] || '',
            min : args[4] || '',
            ss : args[5] || ''
        };
        return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss);
    }

    // 原型
    var Time = function() {
        if (!(this instanceof Time)) {
            return new Time(arguments);
        }
        this.time = paramToArray(arguments);
    };
    //时间差基础单位
    var diffTypes = {
        second: 1000,
        minute: 60000,
        hour: 3600000,
        day: 86400000
    };
    //周单位
    var weeks = ['日','一','二','三','四','五','六'];

    /**
     * [isLeapYear 是否闰年]
     * @param  {[type]}  ele [description]
     * @return {Boolean}     [description]
     */
    function isLeapYear(ele){
        ele = ele || this.time.getFullYear();
        var temp = new Date(ele,2,0).getDate();
        return (temp === 29) ? true : false;
    };
    /**
     * [quantityInMonth 给定月中返回有多少天]
     * @param  {[type]} year  [description]
     * @param  {[type]} month [description]
     * @return {[type]}       [description]
     */
    function quantityInMonth(year,month){
        year = year || this.time.getFullYear();
        month = month || this.time.getMonth();
        console.log(year+'...'+month)
        return new Date(year,month,0).getDate();
    };
    /**
     * [timeDiff 2个时间差]
     * @param  {[type]} startTime [description]
     * @param  {[type]} endTime   [description]
     * @param  {[type]} diffType  [description]
     * @return {[type]}           [description]
     */
    function timeDiff(startTime, endTime, diffType){
        startTime = startTime.replace(/-/g, "/");
        endTime = endTime.replace(/-/g, "/");
        diffType = diffType.toLowerCase();
        var sTime = new Date(startTime), //开始时间
            eTime = new Date(endTime), //结束时间
            divNum = diffTypes[diffType] || 1;
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum, 10), 10);
    }
    /**
     * [getFullTime 格式化时间]
     * @param  {[b]} b [是否使用当前时间]
     * @return {[type]}   [description]
     */
    function getFullTime(b){
        var _time = b ? new Date() : this.time;
        return {
            day: _time.getDate(),
            week: _time.getDay(),
            month: _time.getMonth() + 1,
            year: _time.getFullYear(),
            hours: _time.getHours(),
            minute: _time.getMinutes(),
            seconds: _time.getSeconds(),
            mseconds: _time.getMilliseconds(),
            hab: _time.getFullYear() + '年' + (_time.getMonth() + 1) + '月' + _time.getDate() + '日 星期' + toCweek(_time.getDay()) + ' ' + _time.getHours() + '时' + _time.getMinutes() + '分' + _time.getSeconds() + '秒'
        }
    };

    //countdown
    //参数:单参数从现在时刻到给定的秒后执行callblack() 核心部分其实是不管给定什么时间,最终都要转换成多少秒
    // 
        var countdown = {
            cTime : null,
            conutDown : function(){
                var args = $arr(arguments),
                    len = args.length,
                    callblack = args.pop();
                    if(tool.getType(callblack) !== 'Function'){
                        throw new Error("callblack is undefined.");
                    }
                if(len > 2){//具体时间
                    var diff = Math.floor((new Date(args[0],(args[1] - 1),args[2],args[3],args[4],args[5]).getTime() - new Date().getTime()) / 1000) * 1000;
                    this.outTime = setTimeout(function() {
                        callblack();
                    },diff);
                }else if(len <= 2){//设定秒数倒计时
                    this.outTime = setTimeout(function(){
                        callblack();
                    },args[0]*1000);
                }else{
                    throw new Error("111")
                }
            },
            stop : function(){
                clearTimeout(this.cTime);
            }
        }




    // 扩展
    extendDeep(Time.prototype, {
        version: '1.0',
        getFullTime : getFullTime,
        timeDiff : timeDiff,
        quantityInMonth :quantityInMonth,
        isLeapYear : isLeapYear
    });
    extendDeep(Time.prototype,countdown)
    if (typeof w.Time === 'undefined') {
        w.Time = Time;
    }

})(window);

比较完整的代码,谢谢yanhaijing的帮助

;(function(w) {
    // 深度复制工具
    function extendDeep() {
        var i,
            target = arguments[0] || {},
            astr = '[object Array]',
            toStr = Object.prototype.toString,
            yArr = Array.prototype.slice.call(arguments, 1);
        for (i = 0, len = yArr.length; i < len; i++) {
            var temp = yArr[i];
            for (var j in temp) {
                if (target.hasOwnProperty(j) && (target[i] === temp[i])) {
                    continue;
                }
                if (temp.hasOwnProperty(j)) {
                    if (typeof temp[j] === 'object') {
                        target[j] = (toStr.call(temp[j] === astr)) ? [] : {};
                        extendDeep(target[j], temp[j]);
                    } else {
                        if (typeof temp[j] !== 'undefined') {
                            target[j] = temp[j];
                        }
                    }
                }
            }
        }
        return target;
    }
    // 工具函数区
    var tool = {
        getType : function(ele){
            var oStr = Object.prototype.toString.call(ele),
            reg = /[object (.*)]/,
            arr = reg.exec(oStr);
            return arr[1];
        },
        isArray : function (ele) {
            return (this.getType(ele) === 'Array') ? true : false;
        },
        isObject : function (ele) {
            return (this.getType(ele) === 'Object') ? true : false;
        }
    }
    /**
     * [$arr 数组话函数]
     * @param  {[元素或类数组]} ele [传进的元素或者类数组话]
     * @return {[type]}     [description]
     */
    function $arr(ele) {
      if (!ele){
        return [];
      } 
      if (ele.toArray){
        return ele.toArray();
      } 
      var length = ele.length || 0, results = new Array(length);

      while (length--){
        results[length] = ele[length];
      } 
      return results;
    }
    /**
     * [toCweek 数字转换汉字]
     * @param  {[number]} ele [传入数字]
     * @return {[string]}     [返回汉字]
     */
    function toCweek(ele) {
        if (tool.getType(ele) !== 'Number') {
            ele = Number(ele);
            if (isNaN(ele)) {
                return undefined;
            }
        }
        ele = ele % 7;//大于七的处理
        return weeks[ele];
    }
    //2014,12,10,10,10,10 || '2014,12,10,10,10,10'
    /**
     * [paramToArray 根据参数判断返回何等时间]
     * @param  {[type]} args [arguments]
     * @return {[Date]}      [根据不同的设定时间返回时间对象]
     */
    function paramToArray(args){//扩展次参数操作
        args = $arr(args[0]);
        var arr={};
        if(args.length === 0){
            return new Date();
        }
        if(tool.getType(args[0]) === 'String'){
            var str = args[0],
            arr1 = str.split(',');
            arr = {
                yyyy : arr1[0] || '',
                mth : arr1[1] || '',
                dd : arr1[2] || '',
                hh : arr1[3] || '',
                min : arr1[4] || '',
                ss : arr1[5] || ''
            }
            return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss);
        }
         arr = {
            yyyy : args[0] || '',
            mth : args[1] || '',
            dd : args[2] || '',
            hh : args[3] || '',
            min : args[4] || '',
            ss : args[5] || ''
        };
        return new Date(arr.yyyy,arr.mth,arr.dd,arr.hh,arr.min,arr.ss);
    }

    // 原型
    var Time = function() {
        if (!(this instanceof Time)) {
            return new Time(arguments);
        }
        this.time = paramToArray(arguments);
    };
    //时间差基础单位
    var diffTypes = {
        second: 1000,
        minute: 60000,
        hour: 3600000,
        day: 86400000
    };
    //周单位
    var weeks = ['日','一','二','三','四','五','六'];

    /**
     * [isLeapYear 是否闰年]
     * @param  {[type]}  ele [description]
     * @return {Boolean}     [description]
     */
    Time.isLeapYear = function(ele){
        ele = ele || this.time.getFullYear();
        var temp = new Date(ele,2,0).getDate();
        return (temp === 29) ? true : false;
    };
    /**
     * [quantityInMonth 给定月中返回有多少天]
     * @param  {[type]} year  [description]
     * @param  {[type]} month [description]
     * @return {[type]}       [description]
     */
    Time.quantityInMonth = function(year,month){
        year = year || this.time.getFullYear();
        month = month || this.time.getMonth();
        return new Date(year,month,0).getDate();
    };
    /**
     * [timeDiff 2个时间差]
     * @param  {[type]} startTime [description]
     * @param  {[type]} endTime   [description]
     * @param  {[type]} diffType  [description]
     * @return {[type]}           [description]
     */
     Time.timeDiff = function(startTime, endTime, diffType){
        startTime = startTime.replace(/-/g, "/");
        endTime = endTime.replace(/-/g, "/");
        diffType = diffType.toLowerCase();
        var sTime = new Date(startTime), //开始时间
            eTime = new Date(endTime), //结束时间
            divNum = diffTypes[diffType] || 1;
        return parseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum, 10), 10);
    }
    /**
     * [getFullTime 格式化时间]
     * @param  {[b]} b [是否使用当前时间]
     * @return {[type]}   [description]
     */
    function getFullTime(b){
        var _time = b ? new Date() : this.time;
        return {
            day: _time.getDate(),
            week: _time.getDay(),
            month: _time.getMonth() + 1,
            year: _time.getFullYear(),
            hours: _time.getHours(),
            minute: _time.getMinutes(),
            seconds: _time.getSeconds(),
            mseconds: _time.getMilliseconds(),
            hab: _time.getFullYear() + '年' + (_time.getMonth() + 1) + '月' + _time.getDate() + '日 星期' + toCweek(_time.getDay()) + ' ' + _time.getHours() + '时' + _time.getMinutes() + '分' + _time.getSeconds() + '秒'
        }
    };

    //countdown
    //参数:单参数从现在时刻到给定的秒后执行callblack() 核心部分其实是不管给定什么时间,最终都要转换成多少秒
    // 
        var countdown = {
            cTime : null,
            conutDown : function(){
                var args = $arr(arguments),
                    len = args.length,
                    callblack = args.pop();
                    if(tool.getType(callblack) !== 'Function'){
                        throw new Error("callblack is undefined.");
                    }
                if(len > 2){//具体时间
                    var diff = Math.floor((new Date(args[0],(args[1] - 1),args[2],args[3],args[4],args[5]).getTime() - new Date().getTime()) / 1000) * 1000;
                    this.outTime = setTimeout(function() {
                        callblack();
                    },diff);
                }else if(len <= 2){//设定秒数倒计时
                    this.outTime = setTimeout(function(){
                        callblack();
                    },args[0]*1000);
                }else{
                    throw new Error("111")
                }
            },
            stop : function(){
                clearTimeout(this.cTime);
            }
        }

    // 扩展
    extendDeep(Time.prototype, {
        version: '1.0',
        getFullTime : getFullTime
    });
    extendDeep(Time.prototype,countdown)
    if (typeof w.Time === 'undefined') {
        w.Time = Time;
    }

})(window);
责任编辑:admin  二维码分享:
本文标签: JavaScript
点击我更换图片

评论列表