Skip to content

Loading…

Fix to Issue #1034: Date.parse "parses" dates that should be invalid #1106

Open
wants to merge 4 commits into from
Showing with 34 additions and 2 deletions.
  1. +7 −2 Source/Types/Date.js
  2. +27 −0 Tests/Specs/1.3/Types/Date.js
View
9 Source/Types/Date.js
@@ -367,8 +367,12 @@ Date.extend({
if (!(parsed && parsed.isValid())){
parsed = new Date(nativeParse(from));
- if (!(parsed && parsed.isValid())) parsed = new Date(from.toInt());
+
+ if (!(parsed && parsed.isValid()) && !isNaN(Number(from))) {
+ parsed = new Date(from.toInt());
+ }
}
+
return parsed;
},
@@ -440,7 +444,7 @@ var replacers = function(key){
case 'x': // iso8601 covers yyyy-mm-dd, so just check if month is first
return ((Date.orderIndex('month') == 1) ? '%m[-./]%d' : '%d[-./]%m') + '([-./]%y)?';
case 'X':
- return '%H([.:]%M)?([.:]%S([.:]%s)?)? ?%p? ?%z?';
+ return '%H([.:]%M([.:]%S([.:]%s)?)?)? ?%p? ?%z?';
}
return null;
};
@@ -499,6 +503,7 @@ var build = function(format){
re: new RegExp('^' + re + '$', 'i'),
handler: function(bits){
bits = bits.slice(1).associate(parsed);
+
var date = new Date().clearTime(),
year = bits.y || bits.Y;
View
27 Tests/Specs/1.3/Types/Date.js
@@ -39,6 +39,9 @@ describe('Date', function(){
});
+
+
+
describe('Date.get', function(){
it('should get the hour', function(){
@@ -671,6 +674,29 @@ describe('Date', function(){
Date.prototype.clearTime = clearTime;
});
+ it('should not parse invalid dates', function () {
+ var d = new Date();
+ expect(d.parse('blah').isValid()).toBeFalsy();
+ expect(d.parse('12th blah').isValid()).toBeFalsy();
+ expect(d.parse('47th Oct').isValid()).toBeFalsy();
+ expect(d.parse('2012-55-11').isValid()).toBeFalsy();
+ expect(d.parse('2012-02-88').isValid()).toBeFalsy();
+ expect(d.parse('1981-12-02 37:14:88').isValid()).toBeFalsy();
+ expect(d.parse('1981-12-02 12:99:69').isValid()).toBeFalsy();
+ expect(d.parse('1981-12-02 15:14:71').isValid()).toBeFalsy();
+ });
+
+ it('should not parse invalid dates with custom formats', function () {
+ var d = new Date();
+ var fmt = '%d-%m-%Y %H:%M:%S';
+ Date.defineFormat(fmt);
+ expect(d.parse('02-12-1981 18:00:75').isValid()).toBeFalsy();
+
+ // this passes in Webkit and Firefox, but breaks in IE. IEs native
+ // Date.parse parses dates like '69-7000-1981' as valid dates.
+ expect(d.parse('69-12-1981 18:00:00').isValid()).toBeFalsy();
+ });
+
});
describe('Date.defineFormat', function(){
@@ -705,6 +731,7 @@ describe('Date', function(){
});
+
});
})(this);
Something went wrong with that request. Please try again.