Page 1 of 4 123 ... LastLast
Results 1 to 10 of 32

Thread: [2.1][FIXED] Datefield format bug? d-M-Y not working

  1. #1
    Ext User seymores's Avatar
    Join Date
    Apr 2007
    Posts
    71
    Vote Rating
    0
      0  

    Default [2.1][FIXED] Datefield format bug? d-M-Y not working

    Just upgraded to 2.1 and immediately I found my datefield with custom format doesn't seems to be be right. I have attached a screenshot of the problem.

    The format for my datefield is set to "d-M-Y". According to http://extjs.com/deploy/dev/docs/output/Date.html, this format should accept "21-Apr-2008".

    Anybody else having this problem?
    Attached Images Attached Images

  2. #2
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
      0  

    Default

    could you post your DateField config?

    [edit]
    ok nevermind, i'm seeing other weird behavior... hold on while i look at this...

  3. #3
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
      0  

    Default

    found the problem. my bad for the boo boo

    place this in your overrides file:
    Code:
    if (Ext.version == 2.1) {
        // private
        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 = -1, m = -1, d = -1, h = -1, i = -1, s = -1, ms = -1, o, z, u, v;\n"
              + "input = String(input);var d = new Date();\n"
              + "y = d.getFullYear();\n"
              + "m = d.getMonth();\n"
              + "d = d.getDate();\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" // give top priority to UNIX time
              + "}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))?" // favour UTC offset over GMT offset
              +     " (Ext.type(z) == 'number' ? v.add(Date.SECOND, -v.getTimezoneOffset() * 60 - z) :" // reset to UTC, then add offset
              +         " v.add(Date.MINUTE, -v.getTimezoneOffset() + (sn == '+'? -1 : 1) * (hr * 60 + mn))) : v;\n" // reset to GMT, then add offset
              + "}";
    
          Date.parseRegexes[regexNum] = new RegExp("^" + regex + "$", "i");
          eval(code);
        };
    
        // private
        Ext.apply(Date.parseCodes, {
            j: {
                g:1,
                c:"d = parseInt(results[{0}], 10);\n",
                s:"(\\d{1,2})" // day of month without leading zeroes (1 - 31)
            },
            M: function() {
                for (var a = [], i = 0; i < 12; a.push(Date.getShortMonthName(i)), ++i); // get localised short month names
                return Ext.applyIf({
                    s:"(" + a.join("|") + ")"
                }, Date.formatCodeToRegex("F"));
            },
            n: {
                g:1,
                c:"m = parseInt(results[{0}], 10) - 1;\n",
                s:"(\\d{1,2})" // month number without leading zeros (1 - 12)
            },
            o: function() {
                return Date.formatCodeToRegex("Y");
            },
            g: function() {
                return Date.formatCodeToRegex("G");
            },
            h: function() {
                return Date.formatCodeToRegex("H");
            },
            P: {
              g:1,
              c:[
                  "o = results[{0}];",
                  "var sn = o.substring(0,1);", // get + / - sign
                  "var hr = o.substring(1,3)*1 + Math.floor(o.substring(4,6) / 60);", // get hours (performs minutes-to-hour conversion also, just in case)
                  "var mn = o.substring(4,6) % 60;", // get minutes
                  "o = ((-12 <= (hr*60 + mn)/60) && ((hr*60 + mn)/60 <= 14))? (sn + String.leftPad(hr, 2, '0') + String.leftPad(mn, 2, '0')) : null;\n" // -12hrs <= GMT offset <= 14hrs
              ].join("\n"),
              s: "([+\-]\\d{2}:\\d{2})" // GMT offset in hrs and mins (with colon separator)
            }
        });
    
        // private
        Date.formatCodeToRegex = function(character, currentGroup) {
            // Note: currentGroup - position in regex result array (see notes for Date.parseCodes above)
            var p = Date.parseCodes[character];
    
            if (p) {
              p = Ext.type(p) == 'function'? p() : p;
              Date.parseCodes[character] = p; // reassign function result to prevent repeated execution      
            }
    
            return p? Ext.applyIf({
              c: p.c? String.format(p.c, currentGroup || "{0}") : p.c
            }, p) : {
                g:0,
                c:null,
                s:Ext.escapeRe(character) // treat unrecognised characters as literals
            }
        };
    
        Date.prototype.getGMTOffset = function(colon) {
            return (this.getTimezoneOffset() > 0 ? "-" : "+")
                + String.leftPad(Math.abs(Math.floor(this.getTimezoneOffset() / 60)), 2, "0")
                + (colon ? ":" : "")
                + String.leftPad(Math.abs(this.getTimezoneOffset() % 60), 2, "0");
        };
    }
    [edit 1]
    Also fixed this in SVN rev 2054.

    [edit 2]


    [edit 3]
    fixed "suppressed" minutes / seconds / milliseconds reported here:
    https://www.sencha.com/forum/showthread.php?p=172326

    [edit 4]
    fixed GMT offset issue reported here:
    https://www.sencha.com/forum/showthread.php?p=183737

    [edit 5]
    added Ext 2.1 version check (note: these overrides are not required in Ext 2.2)
    Last edited by mystix; 5 Aug 2008 at 7:54 AM. Reason: update. added note.

  4. #4
    Ext JS Premium Member prophet's Avatar
    Join Date
    Mar 2007
    Location
    Greenwich, CT
    Posts
    189
    Vote Rating
    0
      0  

    Default

    Thanks for the fix. Helped me out!

  5. #5
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
      0  

    Default

    Quote Originally Posted by prophet View Post
    Thanks for the fix. Helped me out!
    feedback on the refactoring would be good

  6. #6
    Ext User seymores's Avatar
    Join Date
    Apr 2007
    Posts
    71
    Vote Rating
    0
      0  

    Thumbs up

    Thanks for the speedy fix!

  7. #7
    Ext User
    Join Date
    Apr 2008
    Posts
    1
    Vote Rating
    0
      0  

    Default Date.parseDate fails if format changes

    I encountered slightly different symptoms, so I want to add them here in case anyone else runs into this.

    I saw Date.parseDate fail mysteriously after adding a TimeField widget to my app. I was able to reproduce the problem with this testcase:

    Code:
    // Returns "4/27/2008 08:00"
    var date1 = Date.parseDate('8:00 AM', 'g:i A'));
    
    // Returns "4/27/2008 16:27" - WRONG!  Date/month are being used as hour/minute.
    var date2 = Date.parseDate('04/27/2008 8:00 AM', 'm/d/Y g:i A');
    After a format specifier is used once, it can never be used again in any other order. This causes subsequent calls to parseDate to fail if the formats don't match.

    Happily, your patch fixes the problem, so I can use TimeFields now. Thank you!

  8. #8
    Ext User
    Join Date
    Apr 2008
    Posts
    1
    Vote Rating
    0
      0  

    Thumbs up Huh

    mystix tnx for the fix. I spend hour to realise what's wrong. I'm beginner in ext so this was nighatmare.
    Thanks again.

  9. #9
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236
    Vote Rating
    5
      0  

    Default

    Quote Originally Posted by tronozac View Post
    mystix tnx for the fix. I spend hour to realise what's wrong. I'm beginner in ext so this was nighatmare.
    Thanks again.
    tip: use the google custom search in the topmost toolbar.

  10. #10
    Ext User
    Join Date
    Mar 2008
    Posts
    2
    Vote Rating
    0
      0  

    Default

    I spent a while trying to work out why the patch wasn

Page 1 of 4 123 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •