Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Allows use of an array of date formats #54

Merged
merged 4 commits into from
Jul 1, 2014
Merged

Allows use of an array of date formats #54

merged 4 commits into from
Jul 1, 2014

Conversation

eddielee6
Copy link
Contributor

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']) and moment('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.

@@ -473,7 +477,7 @@
parseDate: function (value, format) {
var mmnt = null;
if (typeof value === "string") {
mmnt = moment(value, format);
mmnt = moment(value, format, true);
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

Copy link
Member

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);
}

@eddielee6
Copy link
Contributor Author

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.

moment/moment#1746

@eddielee6
Copy link
Contributor Author

Looking at other pull requests this removes the need for this #46

andresmoschini added a commit that referenced this pull request Jul 1, 2014
Allows use of an array of date formats
@andresmoschini andresmoschini merged commit 0ee14c3 into MakingSense:master Jul 1, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants