Page 6 of 6 FirstFirst ... 456
Results 51 to 60 of 60

Thread: [CLOSED-35] [3.x/2.x] DatePicker Picks wrong date

    Thank you for reporting this bug. We will make it our priority to review this report.
  1. #51
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    @pabloidz:

    i see the problem with the override now -- there's a missing semicolon at the end of the for loop. when the code gets minified the next line gets merged into a single line, which will cause problems.

    you can safely remove the empty parentheses and add a single semicolon, like so:
    Code:
    for (var hr = 1, c = this.add(Date.HOUR, hr); c.getDate() != d; hr++, c = this.add(Date.HOUR, hr));
    that should do the trick.

    i'll update the override to include that missing semicolon.

  2. #52
    Ext User
    Join Date
    Apr 2009
    Posts
    20

    Default

    Quote Originally Posted by pabloidz View Post
    Hi there,

    I was getting the same error (another victim of Brazilian Oct 18), with Ext 2.2. I discovered that the clearTime function in this version doesn't account for DST, but the override per se didn't solve the problem.

    I had to change a single line in the function: in the for-loop marked below, I included brackets, and now it does work for any date. I didn't tried it with 2.3+, but let me know if it will solve in subsequent versions:

    Code:
    Date.prototype.clearTime = function(clone) {    
        if (clone) {
            return this.clone().clearTime();
        }     
    
        // get current date before clearing time
        var d = this.getDate();
        
        // clear time
        this.setHours(0);
        this.setMinutes(0);
        this.setSeconds(0);
        this.setMilliseconds(0);
    
        if (this.getDate() != d) { // account for DST (i.e. day of month changed when setting hour = 0)
            // note: DST adjustments are assumed to occur in multiples of 1 hour (this is almost always the ca
            // refer to http://www.timeanddate.com/time/aboutdst.html for the (rare) exceptions to this rule
            // increment hour until cloned date == current date
            for (var hr = 1, c = this.add(Date.HOUR, hr); c.getDate() != d; hr++, c = this.add(Date.HOUR, hr)) {}
            this.setDate(d);
            this.setHours(c.getHours());
        }     
    
        return this; 
    }
    You must add the code above in the same file as the @mystix's patch at second page.
    It works. Thx very mach.

  3. #53
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    @mofo, for completeness, could you state the version of Ext you're currently using?
    i searched through all your posts in this thread but didn't see you / @averta mention any version number.

  4. #54

    Default

    Quote Originally Posted by mystix View Post
    @pabloidz:

    i see the problem with the override now -- there's a missing semicolon at the end of the for loop. when the code gets minified the next line gets merged into a single line, which will cause problems.

    you can safely remove the empty parentheses and add a single semicolon, like so:
    Code:
    for (var hr = 1, c = this.add(Date.HOUR, hr); c.getDate() != d; hr++, c = this.add(Date.HOUR, hr));
    that should do the trick.

    i'll update the override to include that missing semicolon.
    Thanks mystix, just with the single semicolon it already works. I might get confused while testing and debugging

  5. #55
    Ext User
    Join Date
    Apr 2009
    Posts
    20

    Default

    Quote Originally Posted by mystix View Post
    @mofo, for completeness, could you state the version of Ext you're currently using?
    i searched through all your posts in this thread but didn't see you / @averta mention any version number.
    I work with 1.1.1 and 2.x. . 1.1.1 it worked well, too, only need to put the modification on the code from 1.1.1.

  6. #56

    Default

    I'm on 2.3.0, my timezone is -3 (Brazil).
    I did the overrides but I still cannot use the date 17/10/2010.

    Any help?
    Thanks!

  7. #57
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,236

    Default

    Quote Originally Posted by wagner-morais View Post
    I'm on 2.3.0, my timezone is -3 (Brazil).
    I did the overrides but I still cannot use the date 17/10/2010.

    Any help?
    Thanks!
    see post post #49.

  8. #58

    Default

    I'm using Windows 7 Professional, not XP.

  9. #59

    Default Brazilian Code

    Wagner,

    The code posted here works. The full code for use:

    Code:
    Ext.override(Ext.DatePicker, {
        // private
        update : function(date, forceRefresh) {
            var vd = this.activeDate;
    		this.activeDate = date;
    		if (!forceRefresh && vd && this.el) {
                var t = date.getTime();
    			if (vd.getMonth() == date.getMonth() && vd.getFullYear() == date.getFullYear()) {
                    this.cells.removeClass("x-date-selected");
                    this.cells.each(function(c) {
                        if (c.dom.firstChild.dateValue == t) {
                            c.addClass("x-date-selected");
                            setTimeout(function() {
                                try {
                                    c.dom.firstChild.focus();
                                } catch(e) {
                                }
                            }, 50);
                            return false;
                        }
                    });
                    return;
                }
            }
            var days = date.getDaysInMonth();
            var firstOfMonth = date.getFirstDateOfMonth();
            var startingPos = firstOfMonth.getDay() - this.startDay;
    		
            if (startingPos <= this.startDay) {
                startingPos += 7;
            }
    
            var pm = date.add("mo", -1);
    		var prevStart = pm.getDaysInMonth() - startingPos;
    		
            var cells = this.cells.elements;
            var textEls = this.textNodes;
            days += startingPos;
    		
            // convert everything to numbers so it's fast
            // var day = 86400000; // not in use
            var d = (new Date(pm.getFullYear(), pm.getMonth(), prevStart, 12)); // )).clearTime();
    		var today = new Date().clearTime().getTime();
            var sel = date.clearTime().getTime(true);
    		var min = this.minDate ? this.minDate.clearTime(true) : Number.NEGATIVE_INFINITY;
            var max = this.maxDate ? this.maxDate.clearTime(true) : Number.POSITIVE_INFINITY;
            var ddMatch = this.disabledDatesRE;
            var ddText = this.disabledDatesText;
            var ddays = this.disabledDays ? this.disabledDays.join("") : false;
            var ddaysText = this.disabledDaysText;
            var format = this.format;
    
            if (this.showToday) {
                var td = new Date().clearTime();
                var disable = (td < min || td > max ||
                               (ddMatch && format && ddMatch.test(td.dateFormat(format))) ||
                               (ddays && ddays.indexOf(td.getDay()) != -1));
    
                this.todayBtn.setDisabled(disable);
                this.todayKeyListener[disable ? 'disable' : 'enable']();
            }
    
            var setCellClass = function(cal, cell) {
                cell.title = "";
                var t = d.clearTime(true).getTime();
                cell.firstChild.dateValue = t;
    			if (t == today) {
                    cell.className += " x-date-today";
                    cell.title = cal.todayText;
                }
                if (t == sel) {
                    cell.className += " x-date-selected";
                    setTimeout(function() {
                        try {
                            cell.firstChild.focus();
                        } catch(e) {
                        }
                    }, 50);
                }
                // disabling
                if (t < min) {
                    cell.className = " x-date-disabled";
                    cell.title = cal.minText;
                    return;
                }
                if (t > max) {
                    cell.className = " x-date-disabled";
                    cell.title = cal.maxText;
                    return;
                }
    			if (ddays) {
                    if (ddays.indexOf(d.getDay()) != -1) {
                        cell.title = ddaysText;
                        cell.className = " x-date-disabled";
                    }
                }
                if (ddMatch && format) {
                    var fvalue = d.dateFormat(format);
                    if (ddMatch.test(fvalue)) {
                        cell.title = ddText.replace("%0", fvalue);
                        cell.className = " x-date-disabled";
                    }
                }
            };
    
            var i = 0;
            for (; i < startingPos; i++) {
                textEls[i].innerHTML = (++prevStart);
                d.setDate(d.getDate() + 1);
                cells[i].className = "x-date-prevday";
                setCellClass(this, cells[i]);
            }
            for (; i < days; i++) {
                var intDay = i - startingPos + 1;
                textEls[i].innerHTML = (intDay);
                d.setDate(d.getDate() + 1);
                cells[i].className = "x-date-active";
                setCellClass(this, cells[i]);
            }
            var extraDays = 0;
            for (; i < 42; i++) {
                textEls[i].innerHTML = (++extraDays);
                d.setDate(d.getDate() + 1);
                cells[i].className = "x-date-nextday";
                setCellClass(this, cells[i]);
            }
    
            this.mbtn.setText(this.monthNames[date.getMonth()] + " " + date.getFullYear());
    
            if (!this.internalRender) {
                var main = this.el.dom.firstChild;
                var w = main.offsetWidth;
                this.el.setWidth(w + this.el.getBorderWidth("lr"));
                Ext.fly(main).setWidth(w);
                this.internalRender = true;
                // opera does not respect the auto grow header center column
                // then, after it gets a width opera refuses to recalculate
                // without a second pass
                if (Ext.isOpera && !this.secondPass) {
                    main.rows[0].cells[1].style.width = (w - (main.rows[0].cells[0].offsetWidth + main.rows[0].cells[2].offsetWidth)) + "px";
                    this.secondPass = true;
                    this.update.defer(10, this, [date]);
                }
            }
        }
    });
    
    Ext.override(Ext.form.DateField, {
        /**
         * @cfg {Boolean} strict
         * false to allow Date "rollover" for invalid dates (defaults to true)
         */
        strict: true,
    
        // private -- in the absence of a time value, a default value of 12 noon will be used
        // (note: 12 noon was chosen because it steers well clear of all DST timezone changes)
        initTime: '12', // 24 hour format
        
        // private
        initTimeFormat: 'H',
    
        // note: this turned out to be reaaaaaalllly difficult to customize to
        // fit every situation, so what i've put up here is a real naive implementation which 
        // works for the online examples (disclaimer: i've only tried it on one, but most of the configs are similar).
        // i suggest customizing it so it fits your particular situation. ask if you don't understand what on earth
        // i'm trying to achieve with this function
        safeParse : function(value, format, strict) {
            if (/[gGhH]/.test(format.replace(/(\\.)/g, ''))) {
                // if parse format contains hour information, no DST adjustment is necessary
    			return Date.parseDate(value, format, strict);
            } else {
                // set time to 12 noon, then clear the time
    			//return Date.parseDate(value + ' ' + this.initTime, format + ' ' + this.initTimeFormat, strict).clearTime();
    			var nd = Date.parseDate(value + ' ' + this.initTime, format + ' ' + this.initTimeFormat, strict);
    			if (nd != undefined)
    				return nd.clearTime();
            }
        },
    
        parseDate : function(value) {
    		if(!value || Ext.isDate(value)){
                return value;
            }
        
            var v = this.safeParse(value, this.format, this.strict),
                af = this.altFormats;
            
            if (!v && af) {
                if (!this.altFormatsArray) {
                    this.altFormatsArray = af.split("|");
                }
            
                for (var i = 0, afa = this.altFormatsArray, len = afa.length; i < len && !v; i++) {
                    v = this.safeParse(value, afa[i], this.strict);
                }
            }
            return v;
        }
    });
    
    Date.prototype.clearTime = function(clone) {    
        if (clone) {
            return this.clone().clearTime();
        }     
    
        // get current date before clearing time
        var d = this.getDate();
        
        // clear time
        this.setHours(0);
        this.setMinutes(0);
        this.setSeconds(0);
        this.setMilliseconds(0);
    
        if (this.getDate() != d) { // account for DST (i.e. day of month changed when setting hour = 0)
            // note: DST adjustments are assumed to occur in multiples of 1 hour (this is almost always the ca
            // refer to http://www.timeanddate.com/time/aboutdst.html for the (rare) exceptions to this rule
            // increment hour until cloned date == current date
            for (var hr = 1, c = this.add(Date.HOUR, hr); c.getDate() != d; hr++, c = this.add(Date.HOUR, hr)) {}
            this.setDate(d);
            this.setHours(c.getHours());
        }     
    
        return this; 
    }

  10. #60

    Default

    AVerta, Thank you VERY much!
    It works!

Page 6 of 6 FirstFirst ... 456

Posting Permissions

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