Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Django DateTime Field with strftime() method

Tags:

python

django

I am trying to format the string of a DateTimeField contained in one of my models.

I am using this method inside the model:

def HTMLDisplayTime(self):
        time = self.time.date.strftime(%a %H:%M  %d/%m/%y)
        return time

Where "self.time" refers to the DateTimeField in question.

I have tried several variations, omitting some items, with and without .date preceding the .strftime method, and have imported both import datetime and from datetime import datetime.

like image 554
user8951490 Avatar asked Mar 27 '26 07:03

user8951490


1 Answers

Try using quotes around the format argument in strftime() like:

time = self.time.strftime('%a %H:%M  %d/%m/%y')
like image 181
Ankita Gupta Avatar answered Mar 29 '26 19:03

Ankita Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!