Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8000683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:52:48+00:00 2026-06-04T15:52:48+00:00

By default Django displays date/time as something like May 13th, 2010, 4:01 p.m. .

  • 0

By default Django displays date/time as something like May 13th, 2010, 4:01 p.m.. I have also configured a jQuery calendar plugin (I used dateFormat: "MM d, yy", timeFormat: "h:mm tt" and I modified the JavaScript source code to output a.m. and p.m. instead of AM and PM) to display the date/time in the same format.

But the form validation (by default) takes in the form of

DATETIME_INPUT_FORMATS = (
    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M:%S.%f',  # '2006-10-25 14:30:59.000200'
    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30'
    '%Y-%m-%d',              # '2006-10-25'
    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59'
    '%m/%d/%Y %H:%M:%S.%f',  # '10/25/2006 14:30:59.000200'
    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30'
    '%m/%d/%Y',              # '10/25/2006'
    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59'
    '%m/%d/%y %H:%M:%S.%f',  # '10/25/06 14:30:59.000200'
    '%m/%d/%y %H:%M',        # '10/25/06 14:30'
    '%m/%d/%y',              # '10/25/06'
)

I tried to add '%B %d, %Y, %I:%M %p', but I’m not sure if %p accounts for a.m. and p.m. (notice the periods after the letters). Here is a link to Python’s documentation on the date/time formats.

How do you guys do this? Do you just stick with Django’s default (ugly) date/time format or do you somehow convert the time before checking for form.is_valid() (whether in JavaScript or Python)?


EDIT for @Thomas Orozco

I wrote a new form field called CustomDateTimeField:

class CustomDateTimeField(forms.DateTimeField):
    def strptime(self, value, format):
        return datetime.datetime.strptime(value.replace("a.m.", "AM").replace("p.m.", "PM"), "%B %d, %Y, %I:%M %p")

However my form uses ModelForm that has a dynamic fields under class Meta. Here is what I mean:

def order_edit(request, pk):
    order = Order.objects.get(pk=pk)

    if foo:
        include_fields = (
            'fa_date',
            'sa_date',
        )
    else:
        include_fields = (
            'sa_date',
        )

    class OrderDetailForm(forms.ModelForm):
        class Meta:
            model = Order
            fields = include_fields

    if request.method == 'POST':
        form = OrderDetailForm(data=request.POST, instance=order)

where Order.sa_date and Order.fa_date are both models.DateTimeField. And I think I can only use DateTimeInput for ModelForm‘s widgets. So how would I tell the form to use CustomDateTimeField for OrderEditForm? Do I need to add extra arguements via __init__ and manually redefine the fields like self.fields['fa_date'] = CustomDateTimeField(...)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:52:49+00:00Added an answer on June 4, 2026 at 3:52 pm

    Initial answer

    If you want your field to accept a particular format, the simpler solution would be to implement your own form field, which is usually pretty easy, especially here, given that Django handily provides you with an efficient base class.

    Indeed, looking at the django source for the DateTimeField, you can see that it inherits from BaseTemporalField. Therefore, you’ll only need to subclass BaseTemporalField and implement the strptime method so that it accepts your input format.

    Said input format should be passed to your instance through the input_formats variable, though this wouldn’t be a requirement if you’re willing to write a little bit more code.

    You can also wade through the code for DateTimeField and DateField to get some inspiration.

    Edit

    You actually can override the fields of a ModelForm: you just have to redefine them. In your situation, you however can’t use a conditional block to do so (you can’t have a if statement in a class definition!). So you have two solutions:

    class SAOrderDetailForm(forms.ModelForm):
        class Meta:
            model = Order
            fields = ('sa_date',)
        sa_date = CustomDateTimeField()
    
    class FASAOrderDetailForm(SAOrderDetailForm): #Inheritance matters!
        class Meta:
            model = Order
            fields = ('sa_date','fa_date')
        fa_date = CustomDateTimeField()
    
    if foo:
        FormClass = FASAOrderDetailForm
    else:
        FormClass = SAOrderDetailForm
    

    Alternatively, you could use dynamic creation of form classes , but that’s sightly out of the scope of this question, so I’ll leave you with this post from James Bennett that explains how to do so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing something like this in myproject.myapp.urls : from django.conf.urls.defaults import * urlpatterns
I have just started using django-cms so please forgive my newbieness. Every time i
I would like to create a table with django-tables2 such that different rows have
I am using Djangos default authentication system (django.contrib.auth) and I would like to add
HI I want to align the radio buttons horizontally. By default django forms displays
I've noticed that the default Django's datetime as a string looks like this (from
I implemented a simple sitemap class using Django's default sitemap application. As it was
django.utils.translation.get_language() returns default locale if translation is not activated. Is there a way to
Do you know the default isolation level of the transactions used in Django? Is
I am curious about the security of default admin panel of Django. For a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.