Results 1 to 3 of 3

Thread: Incorrect Date parser produced by Ext.Date

    Looks like we can't reproduce the issue or there's a problem in the test case provided.
  1. #1
    Sencha Premium User
    Join Date
    Mar 2009
    Posts
    254

    Default Incorrect Date parser produced by Ext.Date

    I was getting an exception in my application and when I analyzed, it was because of Ext.Date.parse returning undefined for a date string I passed to it (using Ext 4.1):

    Code:
    Ext.Date.parse('2012-06-11T00:00:00', 'Y-m-dTh:i:s')
    
    I copied the parser method using Dev tools from Ext.Date.parseFunctions['Y-m-dTh:i'] and pasted in my javascript file to step-through the problem. Strangely when I did that, the parser method started returning today's date always. Here's the parser method produced by ExtJs:

    Code:
    Ext.Date.parseFunctions['Y-m-dTh:i:s'] = function(input, strict) {
        var dt, y, m, d, h, i, s, ms, o, z, zz, u, v,
    def = Ext.Date.defaults,
    results = String(input).match(Ext.Date.parseRegexes[0]);
        if (results) {
            y = parseInt(results[1], 10);
            m = parseInt(results[2], 10) - 1;
            d = parseInt(results[3], 10);
            h = parseInt(results[4], 10);
            i = parseInt(results[5], 10);
            s = parseInt(results[6], 10);
    
    
            if (u != null) {
                v = new Date(u * 1000);
            } else {
                dt = Ext.Date.clearTime(new Date);
                y = Ext.Number.from(y, Ext.Number.from(def.y, dt.getFullYear()));
                m = Ext.Number.from(m, Ext.Number.from(def.m - 1, dt.getMonth()));
                d = Ext.Number.from(d, Ext.Number.from(def.d, dt.getDate()));
                h = Ext.Number.from(h, Ext.Number.from(def.h, dt.getHours()));
                i = Ext.Number.from(i, Ext.Number.from(def.i, dt.getMinutes()));
                s = Ext.Number.from(s, Ext.Number.from(def.s, dt.getSeconds()));
                ms = Ext.Number.from(ms, Ext.Number.from(def.ms, dt.getMilliseconds()));
                if (z >= 0 && y >= 0) {
                    v = Ext.Date.add(new Date(y < 100 ? 100 : y, 0, 1, h, i, s, ms), Ext.Date.YEAR, y < 100 ? y - 100 : 0);
                    v = !strict ? v : (strict === true && (z <= 364 || (Ext.Date.isLeapYear(v) && z <= 365)) ? Ext.Date.add(v, Ext.Date.DAY, z) : null);
                } else if (strict === true && !Ext.Date.isValid(y, m + 1, d, h, i, s, ms)) {
                    v = null;
                } else {
                    v = Ext.Date.add(new Date(y < 100 ? 100 : y, m, d, h, i, s, ms), Ext.Date.YEAR, y < 100 ? y - 100 : 0);
                }
            }
        }
        if (v) {
            if (zz != null) {
                v = Ext.Date.add(v, Ext.Date.SECOND, -v.getTimezoneOffset() * 60 - zz);
            } else if (o) {
                v = Ext.Date.add(v, Ext.Date.MINUTE, -v.getTimezoneOffset() + (sn == '+' ? -1 : 1) * (hr * 60 + mn));
            }
        }
        return v;
    }
    I need to deliver a piece of code quickly, so did not drill down further rather re-writing this particular parsing method myself. But maybe Ext folks can have a look at this to fix the issue with date parsing.

    EDIT: I think I need to mention that my browser system timezone is set to IST (+05:30 hours). I am sure this should not matter but I was running this code at Jun 17, 2012 between 12:30 am to 1:00 am (just in case Ext date parsing method regexes are based on current system's time).

  2. #2
    Sencha Premium User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    19,258

    Default

    I don't think there's any bug here. "T" is used as a format specifier:

    T Timezone abbreviation of the machine running the code Examples: EST, MDT, PDT
    As such, it's trying to match that as part of your date. If you want "T" to be treated as a literal, then you need to escape it:

    Code:
    console.log(Ext.Date.parse('2012-06-11T00:00:00', 'Y-m-d\\TH:i:s'));

  3. #3
    Sencha Premium User
    Join Date
    Mar 2009
    Posts
    254

    Default

    Hmmm.. okay thanks evant, I somehow overlooked that aspect. This code is undergoing Ext 3 to Ext 4 upgrade in phases and this worked in Ext 3 perfectly. Ext 3 produced the following parser method for the same date format:

    Code:
    function anonymous(input,strict) {var dt, y, m, d, h, i, s, ms, o, z, zz, u, v,def = Date.defaults,results = String(input).match(Date.parseRegexes[1]);if(results){y = parseInt(results[1], 10);m = parseInt(results[2], 10) - 1;d = parseInt(results[3], 10);h = parseInt(results[4], 10);i = parseInt(results[5], 10);s = parseInt(results[6], 10);if(u != null){v = new Date(u * 1000);}else{dt = (new Date()).clearTime();y = Ext.num(y, Ext.num(def.y, dt.getFullYear()));m = Ext.num(m, Ext.num(def.m - 1, dt.getMonth()));d = Ext.num(d, Ext.num(def.d, dt.getDate()));h  = Ext.num(h, Ext.num(def.h, dt.getHours()));i  = Ext.num(i, Ext.num(def.i, dt.getMinutes()));s  = Ext.num(s, Ext.num(def.s, dt.getSeconds()));ms = Ext.num(ms, Ext.num(def.ms, dt.getMilliseconds()));if(z >= 0 && y >= 0){v = new Date(y < 100 ? 100 : y, 0, 1, h, i, s, ms).add(Date.YEAR, y < 100 ? y - 100 : 0);v = !strict? v : (strict === true && (z <= 364 || (v.isLeapYear() && z <= 365))? v.add(Date.DAY, z) : null);}else if(strict === true && !Date.isValid(y, m + 1, d, h, i, s, ms)){v = null;}else{v = new Date(y < 100 ? 100 : y, m, d, h, i, s, ms).add(Date.YEAR, y < 100 ? y - 100 : 0);}}}if(v){if(zz != null){v = v.add(Date.SECOND, -v.getTimezoneOffset() * 60 - zz);}else if(o){v = v.add(Date.MINUTE, -v.getTimezoneOffset() + (sn == '+'? -1 : 1) * (hr * 60 + mn));}}return v;}
    

Posting Permissions

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