From c5b05bb2aff3e17793785a45688be331c8411049 Mon Sep 17 00:00:00 2001 From: Marijus Gudiskis Date: Thu, 6 Jun 2024 15:33:58 +0200 Subject: [PATCH 1/3] month name is converted to number --- js/bootstrap-datepicker.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/js/bootstrap-datepicker.js b/js/bootstrap-datepicker.js index a1df9f9c2..ff34eed3c 100644 --- a/js/bootstrap-datepicker.js +++ b/js/bootstrap-datepicker.js @@ -260,7 +260,7 @@ if (o.startDate instanceof Date) o.startDate = this._local_to_utc(this._zero_time(o.startDate)); else - o.startDate = DPGlobal.parseDate(o.startDate, format, o.language, o.assumeNearbyYear); + o.startDate = DPGlobal.parseDate(o.startDate, format, o.language, o.assumeNearbyYear, o.monthNameToNumber); } else { o.startDate = -Infinity; @@ -271,7 +271,7 @@ if (o.endDate instanceof Date) o.endDate = this._local_to_utc(this._zero_time(o.endDate)); else - o.endDate = DPGlobal.parseDate(o.endDate, format, o.language, o.assumeNearbyYear); + o.endDate = DPGlobal.parseDate(o.endDate, format, o.language, o.assumeNearbyYear, o.monthNameToNumber); } else { o.endDate = Infinity; @@ -286,7 +286,7 @@ o.datesDisabled = o.datesDisabled.split(','); } o.datesDisabled = $.map(o.datesDisabled, function(d){ - return DPGlobal.parseDate(d, format, o.language, o.assumeNearbyYear); + return DPGlobal.parseDate(d, format, o.language, o.assumeNearbyYear, o.monthNameToNumber); }); var plc = String(o.orientation).toLowerCase().split(/\s+/g), @@ -321,7 +321,7 @@ o.orientation.y = _plc[0] || 'auto'; } if (o.defaultViewDate instanceof Date || typeof o.defaultViewDate === 'string') { - o.defaultViewDate = DPGlobal.parseDate(o.defaultViewDate, format, o.language, o.assumeNearbyYear); + o.defaultViewDate = DPGlobal.parseDate(o.defaultViewDate, format, o.language, o.assumeNearbyYear, o.monthNameToNumber); } else if (o.defaultViewDate) { var year = o.defaultViewDate.year || new Date().getFullYear(); var month = o.defaultViewDate.month || 0; @@ -788,7 +788,7 @@ } dates = $.map(dates, $.proxy(function(date){ - return DPGlobal.parseDate(date, this.o.format, this.o.language, this.o.assumeNearbyYear); + return DPGlobal.parseDate(date, this.o.format, this.o.language, this.o.assumeNearbyYear, this.o.monthNameToNumber); }, this)); dates = $.grep(dates, $.proxy(function(date){ return ( @@ -1735,7 +1735,8 @@ leftArrow: '«', rightArrow: '»' }, - showWeekDays: true + showWeekDays: true, + monthNameToNumber: false }; var locale_opts = $.fn.datepicker.locale_opts = [ 'format', @@ -1802,7 +1803,7 @@ } return {separators: separators, parts: parts}; }, - parseDate: function(date, format, language, assumeNearby){ + parseDate: function(date, format, language, assumeNearby, monthNameToNumber){ if (!date) return undefined; if (date instanceof Date) @@ -1901,6 +1902,10 @@ val = parseInt(parts[i], 10); part = fparts[i]; if (isNaN(val)){ + if (part === 'mm' && monthNameToNumber) { + filtered = $(dates[language].monthsShort).filter(match_part); + val = $.inArray(filtered[0], dates[language].monthsShort) + 1; + } switch (part){ case 'MM': filtered = $(dates[language].months).filter(match_part); From cfebe9a91545d2695089a62b68edbadb95308dab Mon Sep 17 00:00:00 2001 From: Marijus Gudiskis Date: Thu, 6 Jun 2024 15:59:54 +0200 Subject: [PATCH 2/3] added unit tests --- js/bootstrap-datepicker.js | 4 +++- tests/suites/formats.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/js/bootstrap-datepicker.js b/js/bootstrap-datepicker.js index ff34eed3c..c0c36323f 100644 --- a/js/bootstrap-datepicker.js +++ b/js/bootstrap-datepicker.js @@ -1904,7 +1904,9 @@ if (isNaN(val)){ if (part === 'mm' && monthNameToNumber) { filtered = $(dates[language].monthsShort).filter(match_part); - val = $.inArray(filtered[0], dates[language].monthsShort) + 1; + if (filtered[0] !== undefined) { + val = $.inArray(filtered[0], dates[language].monthsShort) + 1; + } } switch (part){ case 'MM': diff --git a/tests/suites/formats.js b/tests/suites/formats.js index 39f999aec..ef2e86a54 100644 --- a/tests/suites/formats.js +++ b/tests/suites/formats.js @@ -350,3 +350,36 @@ test('Assume nearby year - this century (+ 13 years, threshold = 30)', patch_dat .datepicker('setValue'); equal(this.input.val(), '02/14/2023'); })); + +test('Convert month `February` to number `02`, monthNameToNumber === true', patch_date(function(Date){ + Date.now = function(){ + return UTCDate(2024, 6, 6).getTime(); + }; + this.input + .val('February/14/2023') + .datepicker({format: 'mm/dd/yyyy', monthNameToNumber: true}) + .datepicker('setValue'); + equal(this.input.val(), '02/14/2023'); +})); + +test('Convert month `Dec` to number `12`, monthNameToNumber === true', patch_date(function(Date){ + Date.now = function(){ + return UTCDate(2024, 6, 6).getTime(); + }; + this.input + .val('Dec/14/2023') + .datepicker({format: 'mm/dd/yyyy', monthNameToNumber: true}) + .datepicker('setValue'); + equal(this.input.val(), '12/14/2023'); +})); + +test('Non existing moth word could not be converted to number, defaults to current month monthNameToNumber === true', patch_date(function(Date){ + Date.now = function(){ + return UTCDate(2024, 6, 6).getTime(); + }; + this.input + .val('Nonsense/14/2023') + .datepicker({format: 'mm/dd/yyyy', monthNameToNumber: true}) + .datepicker('setValue'); + equal(this.input.val(), '07/14/2023'); +})); \ No newline at end of file From 471e284425b7b65ff15914510aa146ca43167e3b Mon Sep 17 00:00:00 2001 From: Marijus Gudiskis Date: Thu, 6 Jun 2024 16:12:35 +0200 Subject: [PATCH 3/3] added documentation for monthNameToNumber --- docs/options.rst | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/options.rst b/docs/options.rst index e7c7df259..ba2eaa19e 100644 --- a/docs/options.rst +++ b/docs/options.rst @@ -15,6 +15,18 @@ Boolean. Default: false Whether or not to close the datepicker immediately when a date is selected. +monthNameToNumber +----------------- + +Boolean. Default: false + +Takes a month name and returns the corresponding month number if set to True. The conversion is used when the date format is 'mm' for the month part. + +The function works by filtering the short month names in the current language to find a match with the input month name. If a match is found, current month will be shown as a number. + +Example: + +If the current language is English and the input month name is "Jun", the function will return 6, since June is the 6th month of the year. assumeNearbyYear ----------------