3π
In templates, there is already a built-in function now
:
Displays the current date and/or time, using a format according to the
given string. Such string can contain format specifiers characters as
described in the date filter section.Example:
It is {% now "jS F Y H:i" %}
In django 1.8, you can use it with as
:
{% now "Y" as current_year %}
{% blocktrans %}Copyright {{ current_year }}{% endblocktrans %}
In python code, there is no django builtin for date, just use the python datetime.date.now()
to make your own customized function.
3π
I think the fastest and cleanest way is to use the localdate function:
from django.utils.timezone import localdate
today = localdate()
Or, there is also localtime, which is the current datetime in the projectβs timezone:
from django.utils.timezone import localtime
today = localtime().date()
However, remember that now().date()
may be different from current date, since it uses UTC:
Note that now() will always return times in UTC regardless of the value of TIME_ZONE; you can use localtime() to get the time in the current time zone.
- [Django]-Is there a way to restrict apps based on IP?
- [Django]-Django Serializer returns JSON for parent class objects only and leave child objects as same?
- [Django]-How to display multi-word app names properly (i.e. with spaces) in Django admin?
2π
Yes you can get the the current date in Python views.py file in any format you want.
In views.py
import datetime
def your(request)
now=datetime.datetime.now()
print("Date: "+ now.strftime("%Y-%m-%d")) #this will print **2018-02-01** that is todays date
- [Django]-Get method classname inside decorator at __init__ in python
- [Django]-Django accessing uploaded files in model custom save, best practice?
- [Django]-Can't debug django app in VS Code with docker database: 'could not translate host name "db" to address: Name or service not known'