[Django] #25857: DateTimeShortcuts.js could try and consume all DATE_INPUT_FORMATS.

10 views
Skip to first unread message

Django

unread,
Dec 3, 2015, 5:57:19 AM12/3/15
to django-...@googlegroups.com
#25857: DateTimeShortcuts.js could try and consume all DATE_INPUT_FORMATS.
-------------------------------+----------------------
Reporter: kezabelle | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Keywords: calendar
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+----------------------
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:
* `handleCalendarCallback`
* `handleCalendarQuickLink`
* `openCalendar`

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).

--
Ticket URL: <https://code.djangoproject.com/ticket/25857>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 5, 2015, 5:57:42 PM12/5/15
to django-...@googlegroups.com
#25857: DateTimeShortcuts.js could try and consume all DATE_INPUT_FORMATS.
-------------------------------+------------------------------------

Reporter: kezabelle | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: calendar | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* stage: Unreviewed => Accepted
* needs_tests: => 0
* needs_docs: => 0


Comment:

My knowledge of localization is limited, but this seems to make sense.
Maybe other experts can offer some advice.

--
Ticket URL: <https://code.djangoproject.com/ticket/25857#comment:1>

Django

unread,
Mar 18, 2024, 3:14:42 AM3/18/24
to django-...@googlegroups.com
#25857: DateTimeShortcuts.js could try and consume all DATE_INPUT_FORMATS.
-------------------------------+------------------------------------
Reporter: Keryn Knight | Owner: nobody
Type: New feature | Status: new
Component: contrib.admin | Version: dev
Severity: Normal | Resolution:
Keywords: calendar | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/25857#comment:2>
Reply all
Reply to author
Forward
0 new messages