Opened 15 years ago

Closed 15 years ago

#3533 closed (duplicate)

Widget to format dates/times

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


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

Attachments (1)

dateortime_widget.patch (4.2 KB) - added by Chris Beaven 15 years ago.
with tests

Download all attachments as: .zip

Change History (6)

Changed 15 years ago by Chris Beaven

Attachment: dateortime_widget.patch added

with tests

comment:1 Changed 15 years ago by Chris Beaven

Has patch: set
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 15 years ago by Adrian Holovaty

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 15 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 15 years ago by Chris Beaven

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 15 years ago by James Bennett

Resolution: duplicate
Status: newclosed

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

Note: See TracTickets for help on using tickets.
Back to Top