1

I have a postgresql table with a timestamp field, among other fields.

How can I convert this timestamp field to a standard datetime python object and get the date like this: "Apr 23"

I know how to do it in the django template (with the 'date' template tag), I need to do in views.py so I can create an array with these dates and pass it to the django template.

1
  • 4
    Well, if you are using a Django model, and you are using a models.DateTimeField(), Django's ORM will take care of the details of translating from the database format to a Python datetime object. Once you have it as a Python datetime object, you can print it out in a variety of ways. Commented Mar 24, 2011 at 21:27

1 Answer 1

3

Something like this:

TIME_FORMAT = "%b %d"
f_str = obj.date.strftime(TIME_FORMAT)

or

TIME_FORMAT = "%b %d"
arr = [d.strftime(TIME_FORMAT) for d in dates]

strftime formatting

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.