0

I want to change the format of the Date in my Django project.

Now it looks like this: "June 3, 2018, 2:19 p.m.", and I want something like: "23,Oct 2018 - 18H23m".

The model that I have its setting this time:

date = models.DateTimeField(auto_now_add=True)

How can I format the date to the way I want? Are there any other parameters that allow me to do this?

I have just started working with django 2 days ago.

I tried to search around but didnt find any answer.

1

2 Answers 2

1

strftime() allows you to format datetimes as you like for printing etc:

import datetime

today = datetime.datetime.today()
print(today.strftime("%d, %b %Y - %HH%Mm"))
>>> 03, Jun 2018 - 16H54m
Sign up to request clarification or add additional context in comments.

Comments

0

Try these following formats with strftime() that returns a string representing the date, controlled by an explicit format string of python. For a complete list of formatting directives: see this section

date = models.DateTimeField(auto_now_add=True)

>>> date.strftime("%d %b, %Y - %Ih%Mm%S %p") # With AM PM
>>> '03 Jun, 2018 - 12h02m49s PM'

>>> date.strftime("%d %b, %Y - %Hh%Mm")
>>> '03 Jun, 2018 - 12h04m'

You can use DATETIME_FORMAT = "d M Y H:i:s" in your settings to change the display format.

1 Comment

Thank a lot!! i used the DATETIME_FORMAT in setting like this "DATETIME_FORMAT = "d, M Y - H:i" thank you all for the help ;)

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.