码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript——dom常用操作-- date操作

时间:2015-02-13 14:37:03      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

    var Dates = {
        //补位
        digit: function (m) {
            return +m < 10 ? ‘0‘ + m : m;
        },
        //日期格式化可date可是数组也可以是字符串
        parse: function (date, format) {
            var _index = 0;
            if (typeof date == ‘string‘) date = this.toarry(date);
            if (date == null) return ‘‘;
            //console.log(date);
            format = format || ‘YYYY-MM-dd HH:mm:ss‘;
            return format.replace(/YYYY|MM|dd|HH|mm|ss/g, function (str, index, s) {
                return Dates.digit(date[_index++]);
            });
        },
        //获指定日期
        now: function (timestamp, format) {
            var _parsestr, _date = new Date((timestamp | 0) ? function () {
                var _datenow = new Date();
                if (timestamp >= 86400000) {
                    return timestamp + _datenow.getHours() * 3600000 + _datenow.getMinutes() * 60000 + _datenow.getSeconds() * 1000;
                }
                return 86400000 * timestamp + _datenow.getTime();
            }(timestamp) : new Date);
            _parsestr = [_date.getFullYear(), (_date.getMonth() + 1), _date.getDate(), _date.getHours(), _date.getMinutes(), _date.getSeconds()];
            return {
                //返回字符串
                Totime: this.parse(_parsestr, format),
                //返回数组
                Toarry: _parsestr
            };
        },
        //日期序列化
        toarry: function (timestr) {
            var aryy = this.Isdate(timestr, true);//日期正确返回日期数组[YYYY,MM,dd,HH,mm,ss]
            if (!aryy) return null;
            return aryy;
        },
        //验证日期,设置第二个参数可以返回现在日期数组[YYYY,MM,dd,HH,mm,ss]
        Isdate: function (datestr, isreturn) {
            var _DateReg = /^((\d{4})(-|\/)(\d{1,2})\3(\d{1,2}))( (\d{1,2}):(\d{1,2}):(\d{1,2}))?$/,//2015-1-43 3:3:44||2015-1-43 99:99:99
                _Date,
                _result,
                _YYYY, _MM, _dd, _HH = 0, _mm = 0, _ss = 0, _istrue = false;
            if (_DateReg.test(datestr)) {//验证格式正确
                _result = _DateReg.exec(datestr);//datestr.match(reg);
                //console.log(_result);
                if (_result == null) return false;
                _YYYY = _result[2];
                _MM = +_result[4] - 1;
                _dd = _result[5];
                if (_result[6] != undefined && _result[6] != ‘‘) {
                    _HH = _result[7];
                    _mm = _result[8];
                    _ss = _result[9];
                }
                _Date = new Date(_YYYY, _MM, _dd, _HH, _mm, _ss);
                _istrue = (_Date.getFullYear() == _YYYY && _Date.getMonth() == _MM && _Date.getDate() == _dd && _Date.getHours() == _HH && _Date.getMinutes() == _mm && _Date.getSeconds() == _ss);
                return (_istrue && (isreturn && [_YYYY, (_MM + 1), _dd, _HH, _mm, _ss])) || _istrue;
            }
            else {
                return false;
            }
        },
        //润年判断
        LeapYear: function (year) {
            if ((year % 100 == 0 && year % 400 == 0) || year % 4 == 0) return true;
            return false;
        }
    };

  

    //公共方法
    var Common = {
        each: function (arrydom, fn) {
            var len = arrydom.length;
            for (var i = 0; i < len; i++) {
                if (typeof fn == ‘function‘) {
                    fn.call(arrydom[i]);
                }
            }
        },
        //绑定事件
        on: function (dom, even, fn) {
            dom.attachEvent ? dom.attachEvent(‘on‘ + even, function (e) {
                e = e || window.event;
                fn.call(dom, e);
            }) : dom.addEventListener(even, function (e) {
                e = e || window.event;
                fn.call(dom, e);
            }, false);
        },
        //获得属性
        attr: function (dom, attr, val) {
            var str = ‘‘, s;
            if (val) {
                dom.setAttribute(this.trim(attr), val);
                attr.toLowerCase() == ‘class‘ && (dom.className = val);
                attr.toLowerCase() == ‘style‘ && (dom.style.cssText = val);
            }
            else {
                s = dom.getAttribute(attr);
                str = s;
                attr.toLowerCase() == ‘class‘ && (str = dom.className);
                attr.toLowerCase() == ‘style‘ && (str = dom.style.cssText);
                return this.trim(str);
            }
        },
        //删除属性
        removeattr: function (dom, attr) {
            dom.removeAttribute(attr);
        },
        //判断是否存在class
        hasClass: function (dom, str) {
            return new RegExp(‘\\b‘ + str + ‘\\b‘).test(dom.className);
        },
        //添加class
        addClass: function (dom, cls) {
            this.hasClass(dom, cls) || (dom.className += ‘ ‘ + cls);
            dom.className = this.trim(dom.className);
        },
        //删除class
        removeClass: function (dom, cls) {
            var c = this.trim(cls), reg = new RegExp(‘\\b‘ + c + ‘\\b‘);

            if (this.hasClass(dom, c)) {
                dom.className = this.trim(dom.className.replace(reg, ‘‘));
            }
        },
        //去除前后空格
        trim: function (s) {//去除多于空格
            s = s || ‘‘;
            return s.replace(/^\s*|\s*$/g, ‘‘);
        }
    };

  

Javascript——dom常用操作-- date操作

标签:

原文地址:http://www.cnblogs.com/yc-code/p/4290112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!