Code

Opened 7 years ago

Closed 7 years ago

#3533 closed (duplicate)

Widget to format dates/times

Reported by: SmileyChris Owned by: nobody
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Patch for a widget which provides a way to format dates / times when rendering.

Attachments (1)

dateortime_widget.patch (4.2 KB) - added by SmileyChris 7 years ago.
with tests

Download all attachments as: .zip

Change History (6)

Changed 7 years ago by SmileyChris

with tests

comment:1 Changed 7 years ago by SmileyChris

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by adrian

What's the use case for this? It calls to mind this django-users thread, but that was a request for a hidden field, whereas this patch uses an <input type="text">.

comment:3 Changed 7 years ago by jkocherhans

For what it's worth, I've done the same thing, but just wrote a custom widget. My use case was to avoid times being rendered as 23:30:00 for instance. Seconds are irrelevant and I want to display a 12 hours clock with AM/PM. Something more like 9:00 PM. Currently, newforms will render that as 21:00:00. It doesn't matter what formats the widget accepts, it always renders them the same by basically calling str(value)

class TimeWidget(forms.TextInput):
    def render(self, name, value, attrs=None):
        # Midnight, time(0, 0) is False. Check for that in addition to an *actual* missing value.
        if value or value == time(0, 0):
            value = value.strftime("%I:%M %p")
             # Strip off leading 0's. Should be part of strftime, but whatever.
            if value[0] == '0': value = value[1:]
        return super(TimeWidget, self).render(name, value, attrs)

comment:4 Changed 7 years ago by SmileyChris

I know lots of people (including some of my clients) don't like the default output format of YYYY-MM-DD. This gives them a way to change the output for dates (or times).

On a side note to jkocherhans: you can do "%#I" to strip leading 0 off %I

comment:5 Changed 7 years ago by ubernostrum

  • Resolution set to duplicate
  • Status changed from new to closed

#3672 has patches and a bit more discussion for what seems to be the same issue (formatting dates for display).

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.