Opened 11 years ago
Last modified 10 days ago
#25857 new New feature
DateTimeShortcuts.js could try and consume all DATE_INPUT_FORMATS.
| Reported by: | Keryn Knight | Owned by: | |
|---|---|---|---|
| Component: | contrib.admin | Version: | dev |
| Severity: | Normal | Keywords: | calendar |
| Cc: | django@… | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Relates to: #25856, #25024, #23049
Currently, there are 3 uses of DATE_INPUT_FORMATS in the DateTimeShortcuts part of the JS calendar widget.
Their use is always get_format('DATE_INPUT_FORMATS')[0] which means if your first DATE_INPUT_FORMAT is not JS compatible (see, eg: #25856, #23049) the string written back to the input will include undefined
Here's a proposed proof-of-concept solution to get a first compatible match which would pass on both JS and Python sides.
The slightly complexity lies in the fact that rather than an exception, new Date('undefined') actually just yields a string of Invalid Date when used. However, even when given valid a date without a time portion, getTime should always yield either NaN or an integer.
DateTimeShortcuts.tryFormats = function(date) {
var dateFormats = get_format('DATE_INPUT_FORMATS');
for (var i=0; i < dateFormats.length; i++) {
var currentFormat = dateFormats[i];
var dateStr = date.strftime(currentFormat);
var madeDate = new Date(dateStr);
if (isNaN(madeDate.getTime()) === false) {
return {format: currentFormat, value: dateStr};
}
}
return {format: void(0), value: void(0)};
DateTimeShortcuts.bestDateFormat = DateTimeShortcuts.tryFormats(new Date())['format'];
(NB: bestDateFormat would perhaps be better served by being set in DateTimeShortcuts.init but whatever)
and then usages of get_format('DATE_INPUT_FORMATS')[0] could be replaced with DateTimeShortcuts.bestDateFormat in:
handleCalendarCallbackhandleCalendarQuickLinkopenCalendar
Not sure what would be best in the case where there are no valid formats (format would be undefined)
So, given the following:
"DATE_INPUT_FORMATS": [ "%B %d, %Y", "%d/%m/%Y", "%m/%d/%Y" ]
the best match would be "%d/%m/%Y" because %B is unsupported (#25856).
Change History (8)
comment:1 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 3 years ago
| Cc: | added |
|---|
comment:3 by , 3 months ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:4 by , 6 weeks ago
Hi, I've claimed this ticket and plan to work on it.
#25856 and #23049 added %B, %a and %A to the JS strftime, but formats using %j, %U or %W still aren't supported. If the first DATE_INPUT_FORMATS entry uses one of those, the picker still writes "undefined" into the input.
My plan:
- Add a helper that returns the first DATE_INPUT_FORMATS entry the JS strftime fully supports, checking the format string rather than parsing with new Date().
- Use it where get_format('DATE_INPUT_FORMATS')[0] is used now.
- Fall back to %Y-%m-%d if none are supported.
- Add QUnit tests.
Is %Y-%m-%d an acceptable fallback? Happy to adjust if the code has changed since the ticket was opened.
comment:5 by , 2 weeks ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:6 by , 11 days ago
Hi, I’d like to work on this ticket. I see Vaibhav Pant’s plan from comment 4 was unclaimed, and the fallback question is still open. I’ll first reproduce the bug on main by setting DATE_INPUT_FORMATS to start with an unsupported format like %j. Is it okay for me to pick this up, and is %Y-%m-%d an acceptable fallback?
comment:7 by , 11 days ago
| Cc: | removed |
|---|
comment:8 by , 10 days ago
I tested this on current main (commit from this week) with DATE_INPUT_FORMATS = [“%j/%Y”, “%d/%m/%Y”, “%m/%d/%Y”] in a test project with one DateField in the admin. Clicking the calendar and picking a date filled the field with 2026-11-11, not “undefined.” I may be missing a step the reporter used, so could someone confirm the exact steps to reproduce?
My knowledge of localization is limited, but this seems to make sense. Maybe other experts can offer some advice.