Yesterday I lost 2 hours of my life trying to find why AJAX client library function "Date.parseInvariant(value, format)" does not work.
This is a beautiful function allows us to convert strings into Date objects in javascript. It allows us to specify exact format of a string in .NET way.
For example:
Date.parseInvariant("01/12/2008", "dd/MM/yyyy") will return valid date pointing to 1st of December 2008, and
Date.parseInvariant("01/12/20081", "dd/MM/yyyy") will return null ! Yes this is wonderful feature - there is no NaN result, no crazy dates - everything right like in .NET(almost everything :) )
But the function did not work. I debugged the Ajax library script and found the problem on the line:
var result = new Date(); if (year === null) { year = result.getFullYear(); } if (month === null) { month = result.getMonth(); } if (date === null) { date = result.getDate(); } result.setFullYear(year, month, date); if (result.getDate() !== date) return null; if ((weekDay !== null) && (result.getDay() !== weekDay)) { return null; }
After execution of line
result.setFullYear(year, month, date);
the result date was not equal to date we just assigned it!
After long searches and guesses I have found the following script block in javascript file used by "The DHTML Calendar" by www.dynarch.com :
Date.prototype.__msh_oldSetFullYear = Date.prototype.setFullYear; Date.prototype.setFullYear = function(y) { var d = new Date(this); d.__msh_oldSetFullYear(y); if (d.getMonth() != this.getMonth()) this.setDate(28); this.__msh_oldSetFullYear(y); }; // END: DATE OBJECT PATCHES
The "Object patch" patched too much. It replaced setFullYear standard Date javascript function with it's own, completely ignoring two additional parameters that are passed into it.
I solved it by removing the "patch" - commenting it out. It worked for me, as I tested no critical functionality was broken for simple calendar dates selection. You can try to remove/replace the "patch" with more care, I just want to point to this problem with this post.
And in the end I want to thank the dynarch.com team - the calendar is great control anyway. I like it and use in my personal projects. Just yesterday was a little angry about this issue :P.
Hope this helps.
2 comments:
Very helpful, thanks !!!
Thanks. I was struggling with this problem myself.
Post a Comment