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 8564805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:16:52+00:00 2026-06-11T17:16:52+00:00

I’m having a problem saving a datetime object from a form. The form widget

  • 0

I’m having a problem saving a datetime object from a form. The form widget gives me a text string like this: 2012-09-20 04:41 PM

I’m trying to override the clean method in the form, like this:

class LegForm(ModelForm):
    formfield_callback = make_custom_field

    class Meta:
        model = Leg
        exclude = ('riders', 'drivers', 'open_to_prefs','carpool')

    widgets = { 'days': forms.CheckboxSelectMultiple }

    def clean_drive_date_time(self):
        twelve_hour = self.cleaned_data['drive_date_time']
        print "drive time: " + str(twelve_hour)
        data = datetime.strptime(twelve_hour, "%Y-%m-%d %I:%M %p")
        print "24 hour drive time: " + str(data)
        return data

the strptime statement works from the command line and gives me a valid datetime object, but when I try it in the project as shown above, I get an error on my form telling me ” Enter a valid date/time.”

I can save the datetime no problem without a override of the clean method if I use 24 hour format, but that’s more difficult for the end user. Any help is appreciated. Thanks!

  • 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-11T17:16:53+00:00Added an answer on June 11, 2026 at 5:16 pm

    The job of the form field is to take an input from the user and normalize it to a python value. A python’s datetime object does not care if its 12/24 hour formats. All it stores is a datetime in some universal way, which you can represent in either 12/24 formats.

    So from what I understand you are trying to do is to get input from users in 24 hour hour, instead of 12 hour format. The easiest way is to tell the django form field the format you expect from users and it will automatically convert the string from the user to a python datetime object.

    class LegForm(ModelForm):
        formfield_callback = make_custom_field
    
        # overwrite the default DateTimeField with defaults parameters
        drive_date_time = DateTimeField(input_formats=['%Y-%m-%d %I:%M %p'])
    
        class Meta:
            model = Leg
            exclude = ('riders', 'drivers', 'open_to_prefs','carpool')
    
        widgets = { 'days': forms.CheckboxSelectMultiple }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I am currently running into a problem where an element is coming back from

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.