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

  • Home
  • SEARCH
  • 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 733531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:16:05+00:00 2026-05-14T07:16:05+00:00

I am storing dates as an integer field in the format YYYYMMDD, where month

  • 0

I am storing dates as an integer field in the format YYYYMMDD, where month or day is optional.

I have the following function for formatting the number:

def flexibledateformat(value):
    import datetime, re
    try:
        value = str(int(value))
    except:
        return None
    match = re.match(r'(\d{4})(\d\d)(\d\d)$',str(value))
    if match:
        year_val, month_val, day_val = [int(v) for v in match.groups()]
    if day_val:
        return datetime.datetime.strftime(datetime.date(year_val,month_val,day_val),'%b %e, %Y')
    elif month_val:
        return datetime.datetime.strftime(datetime.date(year_val,month_val,1),'%B %Y')
    else:
        return str(year_val)

Which results in the following outputs:

>>> flexibledateformat(20100415)
'Apr 15, 2010'
>>> flexibledateformat(20100400)
'April 2010'
>>> flexibledateformat(20100000)
'2010'

So I’m wondering if there’s a function I can add under the model field class that would automatically call flexibledateformat.

So if there’s a record
r = DataRecord(name=’foo’,date=20100400)
when processed in the form the value would be 20100400 but when output in a template using {{ r.date }} it shows up as “April 2010”.

Further clarification

I do normally use datetime for storing date/time values. In this specific case, I need to record non-specific dates: “x happened in 2009″, “y happened sometime in June 1996″.

The easiest way to do this while still preserving most of the functionality of a date field, including sorting and filtering, is by using an integer in the format of yyyymmdd. That is why I am using an IntegerField instead of a DateTimeField.

This is what I would like to happen:

  1. I store what I call a “Flexible
    Date” in a FlexibleDateField as an
    integer with the format yyyymmdd.
  2. I render a form that includes a
    FlexibleDateField, and the value
    remains an integer so that functions
    necessary for validating it and
    rendering it in widgets work
    correctly.
  3. I call it in a template,
    as in {{ object.flexibledate }} and
    it is formatted according to the
    flexibledateformat rules: 20100416
    -> April 16, 2010; 20100400 -> April 2010; 20100000 -> 2010. This
    also applies when I’m not calling it
    directly, such as when it’s used as
    a header in admin
    (http://example.org/admin/app_name/model_name/).

I’m not aware if these specific things are possible.

Also

I have already written a template filter that can perform this function —
({{object.flexibledate|flexibledateformat}}) —
but I’d rather not have to call the filter every time I want to output one of these values. Except when outputting the form, I pretty much never want to see the number.

  • 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-05-14T07:16:05+00:00Added an answer on May 14, 2026 at 7:16 am

    I think the most djangoish way of accomplishing a similar effect would be to define a custom method on a model:

    class Model(models.Model):
        date = models.IntegerField()
        ...
    
        def flexibledate(self):
            return flexibledateformat(self.date)
    

    You can use it in a template like this:

    {{ r.flexibledate }}
    

    I also wonder why are you using an IntegerField instead of a native DateTimeField.

    Update: You can still use the custom method as an option for list_display in Django Admin. If you want the column header to say something else then simply the method’s name use the “short_description” option, like this:

    class Model(models.Model):
        date = models.IntegerField()
        ...
    
        def flexibledate(self):
            return flexibledateformat(self.date)
        flexibledate.short_description = 'Date'
    

    and then you just use it the same way you would use an ordinary field:

    class YourModelAdmin(admin.ModelAdmin):
        list_display = ('title', 'flexibledate')
    admin.site.register(YourModel, YourModelAdmin)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little blockheaded right now… I have a date string in european format
I have a string containing a date, and another string containing the date format
I have the following as the value for my textbox in SSRS report: =iif(IsNothing(Fields!MyDate.Value),
If I have the following domain object: public class Customer { public virtual Guid
I have a table with one field that can point to a foreign key
I'm storing a simple java.util.date in an Oracle XE database via hibernate. When testing
Can't understand why the following takes place: String date = 06-04-2007 07:05; SimpleDateFormat fmt
Using visual studio BI dev studio 2008. I have a chart that has a
I'm curious how others have offered customizable forms to their website's users (who are
Background, there are several ways to store dates in MySQ. As a string e.g.

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.