发新话题
打印

[合理化建议内容] mootools

本主题由 yidabu 于 2008-3-13 16:34 移动
呵呵,看到有人建议多搞点jquery, 我也建议多搞点mootools.
我个人觉得mootools之强悍已经无法用言语形容了,实力是强到偶想哭。

现贴一段我写的基于mootools.r820的 Native Date, 可以按照php date函数的规则分析和输出日期。
复制内容到剪贴板
代码:
Native(Date);
Native.setFamily({'date': Date});

Date.Names        = {
        Apm:        ['Am','Pm'],
        Suffix:        ['St','Nd','Rd', 'Th'],
        Day:        ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
        Month:        ['January','February','March','April','May','June','July','August','September','October','November','December']
}

Date.ShortNames = {}

Date.map        = {
        Y:{k:'FullYear',l:4},
        y:{k:'FullYear', l:2, a: 2000},
        m:{k:'Month',l:2, a: -1},
        n:{k:'Month',l:2, z:1, a: -1},
        F:{k:'Month', w:true , a: -1},
        M:{k:'Month', w:true , l:3, a: -1},
        w:{k:'Day', l:1},
        l:{k:'Day', w:true },
        D:{k:'Day', w:true ,l:3},
        d:{k:'Date', l:2},
        j:{k:'Date', l:2, z:1},
        S:{k:'Suffix',w:true, low: true},
        a:{k:'Apm', w:true, low: true},
        A:{k:'Apm', w:true},
        g:{k:'Hours',l:2,z:1,m:12},
        G:{k:'Hours',l:2,z:1,m:24},
        h:{k:'Hours',l:2,m:12},
        H:{k:'Hours',l:2,m:24},
        i:{k:'Minutes',l:2},
        s:{k:'Seconds',l:2},
        U:{k:'Time',l:13,z:1, s:1000}
};

if( Client.Platform.win ) Date.map.U.a =   -28799;

Date.RegExps = {};

Date.getRegExp        = function( k ){
        var p = Date.map[k];
        if( !$defined(Date.RegExps[k]) ){
                if( p.w ){
                        var Names = Date.getNames(p);
                        Date.RegExps[k] = new RegExp('^('+ Names.join('|') +')', 'i');
                }else{
                        Date.RegExps[k] = new RegExp('^(\\d' + (p.z? ('{'+p.z+','+p.l+'}') : ('{'+p.l+'}')) + ')', 'g');
                }
        }
        return Date.RegExps[k];
}

Date.getNames        = function(p){
        if( p.l ){
                if( !$defined(Date.ShortNames[p.k]) ){
                        Date.ShortNames[p.k] = Date.Names[p.k].map(function(val){
                                return val.slice(0, p.l);
                        });
                }
                return Date.ShortNames[p.k];
        }else{
                return Date.Names[p.k];
        }
}

Date.extend({       

        rules : 'Y-m-d H:i:s',

        clone: function(){
                return new Date(this.getTime());
        },

        getApm: function(){
                return (this.getHours()>12 ? 1 : 0);
        },

        setApm: function( apm ){
                var h        = this.getHours();
                if( amp & h<=12 ) h -12;
                else if( !amp && h>12 ) h +=12;
                this.setHours(h);
                return this;
        },
       
        getSuffix: function(){
                switch (this.getDate()) {
                        case 1:
                        case 21:
                        case 31:
                                return 0;
                        case 2:
                        case 22:
                                return 1;
                        case 3:
                        case 23:
                                return 2;
                        default:
                                return 3;
                }
        },

        getData: function(k, isNum){
                var p = Date.map[k];
                var val = this['get'+p.k]();
                if( p.w ){
                        val        = Date.getNames(p)[val];
                        if( isNum ) return val;
                        if( $defined(p.low) )  val = val.toLowerCase();
                }else{
                        if (p.s) val = Math.ceil(val / p.s);
                        if ( p.a )        val -= p.a;
                        if( p.m ) val = val % p.m;                       
                        if( isNum ) return val;
                        val        = val.toString();
                        if( val.length > p.l) val = val.slice( -p.l);
                        else if( val.length < p.l && !$defined(p.z) ){                               
                                val = '00000'.slice(0, p.l - val.length) + val;
                        }
                }
                return val;
        },
       
        setData: function(k, v){
                var p = Date.map[k], v =  parseInt(v) || 0;
                if( Date.prototype.hasOwnProperty( 'set'+p.k ) ){
                        this['set'+p.k]( parseInt(this['get'+p.k]()) + (p.s ? v * p.s : v ) );
                }
                return this;
        },

       
        get: function( rules ){
                rules = rules || this.rules ;
                var s = '';
                for(var l = rules.length,i=0;i<l;i++){
                        var k        = rules.charAt(i);
                        if( k === '\\' ){
                                s        += rules.charAt(++i);
                                continue ;
                        }
                        if( Date.map[k] ){
                                s        += this.getData(k);
                        }else{       
                                s        += k;
                        }
                }
                return s;
        },

        set: function( data, rules , isTmp){
                data        = data.toString() || '';
                rules = rules || this.rules;
                if( !isTmp ) this.rules        = rules;
                var p , val;
                for(var l = rules.length,i=0 ; i<l; i++){
                        var k        = rules.charAt(i);
                        if( k === '\\' || !(p = Date.map[k]) ){
                                data = data.substr(1);
                                continue ;
                        }
                        data        = data.replace( Date.getRegExp(k) , '');
                        if( (val =  RegExp.$1) ){
                                if( p.w ) {               
                                        val = Date.getNames(p).indexOf( val.capitalize() );
                                }else{
                                        val = parseInt( val.replace(/^0+/,'')) || 0;
                                        if( p.a ) val += p.a;
                                        if (p.s) val = val * p.s;
                                }
                                if( Date.prototype.hasOwnProperty( 'set'+p.k ) ) this['set'+p.k]( val );
                        }
                 }
                return this;
        }
});
本帖最近评分记录
  • yidabu +20 精品文章 2007-8-2 05:15

TOP

例子
复制内容到剪贴板
代码:
var format = "Y-m-d H:i:s";
var myDate = new Date().set(<?=date("Y-m-d H:i:s")?>, format);
// 这样 myDate 就取得了php中输出的日期,它也可以分析英语月份,am/pm,
myDate.get("F d Y H:i:s");
// 这样就按照指定格式输出了myDate
// 如果你设置了中文或其他语言的 Date.Names, 你可以用Date.set分析相同format的Date.get输出的数据

myDate.setData('Y', -1);
//这样是 myDate的当前年份减1, 同理可加减作月份,日,小时,分,秒
该扩展几乎和php date完全兼容,可以很方便的分析日期。

TOP

发新话题