-
Notifications
You must be signed in to change notification settings - Fork 63
Conversation
This reverts commit 3a5a169.
@@ -473,7 +477,7 @@ | |||
parseDate: function (value, format) { | |||
var mmnt = null; | |||
if (typeof value === "string") { | |||
mmnt = moment(value, format); | |||
mmnt = moment(value, format, true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @eddielee6, thanks by your PR, it is a nice feature!
My only concern is about the third boolean parameter (strict parsing). From Moment.js documentation:
Moment's parser is very forgiving, and this can lead to undesired behavior. As of version 2.3.0, you may specify
a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and
input match exactly.moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false moment('2012-05-25', 'YYYY-MM-DD', true).isValid(); // true
It means that our old parsing behavior is going to change.
Another alternative is to use strict parsing only for array of date formats and not for string format.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I have put this in is because for the date "05/05/45" moment("05/05/45", "DD-MM-YYYY")
successfully parses this as "05/05/0045" rather than failing or returning the intended date of "05/05/2045". So if the date format is specified as ["DD/MM/YYYY", "DD/MM/YY"]
it will successfully parse the date with the first format and not try the second one, even though the date is clearly incorrect.
The only way I found to solve this is to enable strict parsing.
I'm not sure what issues the strict parsing would have in other cases however - so maybe it would be best to just enable it for arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it could just be a parameter the datepicker accepts to enable/disable strict parsing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I propose adding a global setting to enable/disable strict parsing for the date picker. I believe my issue of moment("05/05/45", ["DD/MM/YYYY", "DD/MM/YY"])
returning "05/05/0045" rather than "05/05/2045" is actually an issue with moment.
What do you think to this solution?
I have opened an issue with moment for the format array parsing order moment/moment#1746
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, @eddielee6, I do not see any new commit in this PR.
I understand the problem, and I am not sure if there are an easy and cheap way to fix it at momentjs side.
What if we enable strict parsing only for format as array, something like this:
if (Object.prototype.toString.call(format) === '[object Array]') {
mmnt = moment(value, format, true);
} else {
mmnt = moment(value, format);
}
I have updated the PR with your suggestion for enabling strict parsing only for dates. However moment have acknowledged the issue with the date parser and will hopefully resolve it in a future version. |
Looking at other pull requests this removes the need for this #46 |
Allows use of an array of date formats
Moment.js allows use of an array of date formats when parsing dates.
This means that you can do...
moment('05/05/1945', ['dd/mm/yyyy', 'dd-mm-yyy'])
andmoment('05-05-1945', ['dd/mm/yyyy', 'dd-mm-yyy'])
and both will work. Moment will keep trying the formats in the array until it finds the correct one to parse it with.I have updated the date picker to accept an array for the 'format' parameter. It then uses the first format in the array as the display format.