代码改变世界

Ext DateField 选择二月的bug

2009-07-31 17:24  午夜瞎想  阅读(1478)  评论(2编辑  收藏  举报

当 DateField 的format属性被设置为‘Y-m’的时候 ,找个看似没有任何问题的设置 ,其实缺存在一个小BUG!
比如 :当你选2月份,并且当前系统 日期为 每 个月的29-31号时,BUG就出现了 ,不管你怎么选 ,文本框始终是3月,这样用户在这几天 就不能查看2月份或其他没有当前日期号数的月份都会出不来 .
本来我想选择2月份,


当我点击2月份的一个日期后

这儿还是3月份,
当前系统时间:2009-3-31
如果把系统时间换成 其他小于29日的都可以用
不能用用户这几天不查吧,最后找到一个方法
Ext.apply(Date,{
    createParser : function(format) {
        var funcName = "parse" + Date.parseFunctions.count++;
        var regexNum = Date.parseRegexes.length;
        var currentGroup = 1;
        Date.parseFunctions[format] = funcName;
        var code = "Date." + funcName + " = function(input){\n"
            + "var y, m, d, h = 0, i = 0, s = 0, ms = 0, o, z, u, v;\n"
            + "input = String(input);\n"
            + "d = new Date();\n"
            + "y = d.getFullYear();\n"
            + "m = d.getMonth();\n"
+ "d = 1;\n"                                                        //原代码 + "d = d.getDay();\n"
            + "var results = input.match(Date.parseRegexes[" + regexNum + "]);\n"
            + "if (results && results.length > 0) {";
        var regex = "";
        var special = false;
        var ch = '';
        for (var i = 0; i < format.length; ++i) {
            ch = format.charAt(i);
            if (!special && ch == "\\") {
                special = true;
            }
            else if (special) {
                special = false;
                regex += String.escape(ch);
            }
            else {
                var obj = Date.formatCodeToRegex(ch, currentGroup);
                currentGroup += obj.g;
                regex += obj.s;
                if (obj.g && obj.c) {
                    code += obj.c;
                }
            }
        }
        code += "if (u){\n"
            + "v = new Date(u * 1000);\n"             + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0 && ms >= 0){\n"
            + "v = new Date(y, m, d, h, i, s, ms);\n"
            + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0 && s >= 0){\n"
            + "v = new Date(y, m, d, h, i, s);\n"
            + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0 && i >= 0){\n"
            + "v = new Date(y, m, d, h, i);\n"
            + "}else if (y >= 0 && m >= 0 && d > 0 && h >= 0){\n"
            + "v = new Date(y, m, d, h);\n"
            + "}else if (y >= 0 && m >= 0 && d > 0){\n"
            + "v = new Date(y, m, d);\n"
            + "}else if (y >= 0 && m >= 0){\n"
            + "v = new Date(y, m);\n"
            + "}else if (y >= 0){\n"
            + "v = new Date(y);\n"
            + "}\n}\nreturn (v && (z || o))?"             +     " (Ext.type(z) == 'number' ? v.add(Date.SECOND, -v.getTimezoneOffset() * 60 - z) :"             +         " v.add(Date.MINUTE, -v.getTimezoneOffset() + (sn == '+'? -1 : 1) * (hr * 60 + mn))) : v;\n"             + "}";
        Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
        eval(code);
    }
});

代码是只是把源码中红的那句修改下就OK了