Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calendar.parseDate is not parsing when language is vi and dateFormat: yyyy-M-dd #31

Closed
Srinivas-Alle opened this issue Oct 22, 2019 · 1 comment

Comments

@Srinivas-Alle
Copy link

Srinivas-Alle commented Oct 22, 2019

With the gregorian & Vietnamese language calendar instance, we are unable to parseDate if the date format is yyyy-M-dd.
It is failing only when dates are fallen in months of Tháng 10', 'Tháng 11', 'Tháng 12'

const calendar = $.calendars.instance('gregorian', 'vi')
const date = calendar.parseDate('yyyy-M-dd', '19-Tháng 10-16'); // Throwing invalid literal character


Short names for months in Vietnamese are

monthNamesShort: ['Tháng 1', 'Tháng 2', 'Tháng 3', 'Tháng 4', 'Tháng 5', 'Tháng 6',
			'Tháng 7', 'Tháng 8', 'Tháng 9', 'Tháng 10', 'Tháng 11', 'Tháng 12'],

Since Tháng 10 contains Tháng 1,
line number 303 in jquery.calendars.plus.js has

if (value.substr(iValue, names[i].length).toLowerCase() === names[i].toLowerCase()) {
						iValue += names[i].length;
						return i + calendar.minMonth;
					}

is wrongly calculating and subsequent checkLiteral function is throwing error.

@kbwood
Copy link
Owner

kbwood commented Oct 23, 2019

Yes, it accepts the first match only. I've updated the parseDate function to look for the longest match. You can check out v2.1.2 here, or replace the getName function within parseDate with the following:

			var getName = function(match, shortNames, longNames, step) {
				var names = (doubled(match, step) ? longNames : shortNames);
				var index = -1;
				for (var i = 0; i < names.length; i++) {
					if (value.substr(iValue, names[i].length).toLowerCase() === names[i].toLowerCase()) {
						if (index === -1) {
							index = i;
						} else if (names[i].length > names[index].length) {
							index = i;
						}
					}
				}
				if (index > -1) {
					iValue += names[index].length;
					return index + calendar.minMonth;
				}
				throw ($.calendars.local.unknownNameAt || $.calendars.regionalOptions[''].unknownNameAt).
					replace(/\{0\}/, iValue);
			};

@kbwood kbwood closed this as completed Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants