How do I display current time using Python + Django?

0 views
Skip to first unread message

AlvaroAV via StackOverflow

unread,
Aug 21, 2016, 1:21:05 PM8/21/16
to google-appengin...@googlegroups.com

Maybe this documentation is useful to you: Time Zones

Formatting time in a view

You can get the current time using:

import datetime
now = datetime.datetime.now()

or to get time depending on timezone:

import datetime
from django.utils.timezone import utc

now = datetime.datetime.utcnow().replace(tzinfo=utc)

to format the time you can do:

import datetime

now = datetime.datetime.now().strftime('%H:%M:%S')  #  Time like '23:12:05'

Formatting time in a template

You can send a datetime to the template, let's supose you send a variable called myDate to the template from the view. You could do like this to format this datetime:

{{ myDate | date:"D d M Y"}}  # Format Wed 09 Jan 2008
{{ myDate | date:"SHORT_DATE_FORMAT"}}  # Format 09/01/2008
{{ myDate | date:"d/m/Y"}}  # Format 09/01/2008

Check the Template filter date

I hope this is useful to you



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/5110352/how-do-i-display-current-time-using-python-django/19266907#19266907
Reply all
Reply to author
Forward
0 new messages