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

#2699 month name to number #2703

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------------
Expand Down
21 changes: 14 additions & 7 deletions js/bootstrap-datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -1735,7 +1735,8 @@
leftArrow: '«',
rightArrow: '»'
},
showWeekDays: true
showWeekDays: true,
monthNameToNumber: false
};
var locale_opts = $.fn.datepicker.locale_opts = [
'format',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1901,6 +1902,12 @@
val = parseInt(parts[i], 10);
part = fparts[i];
if (isNaN(val)){
if (part === 'mm' && monthNameToNumber) {
filtered = $(dates[language].monthsShort).filter(match_part);
if (filtered[0] !== undefined) {
val = $.inArray(filtered[0], dates[language].monthsShort) + 1;
}
}
switch (part){
case 'MM':
filtered = $(dates[language].months).filter(match_part);
Expand Down
33 changes: 33 additions & 0 deletions tests/suites/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}));