49👍
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
18👍
Use the now template tag. For example:
{% now "jS F Y H:i" %}
but you’ll need to send your string through template engine before sending the response for it to work.
- [Django]-How to get username from Django Rest Framework JWT token
- [Django]-Django filter the model on ManyToMany count?
- [Django]-Order by count of a ForeignKey field?
12👍
For Django code, not in template the support is actually quite simple.
In settings change the timezone:
TIME_ZONE = 'Asia/Kolkata'
And where you need to use, use the following code:
from django.utils import timezone
now = timezone.now()
Source: https://docs.djangoproject.com/en/2.1/topics/i18n/timezones/
- [Django]-Django – show the length of a queryset in a template
- [Django]-Auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'
- [Django]-How to exempt CSRF Protection on direct_to_template
0👍
You can use time.strftime() for printing the current time. In your urlpatterns, just change '^time/$'
to '^/$'
to map the root page to your time function.
- [Django]-How to run Debug server for Django project in PyCharm Community Edition?
- [Django]-Add Serializer on Reverse Relationship – Django Rest Framework
- [Django]-Why there are two process when i run python manage.py runserver