Returns 'Morning', 'Afternoon', or 'Evening' in the local timezone (specified in settings). Required pytz. 0000-1200 considered morning, 1200-1800 considered afternoon, 1800-0000 considered evening.
1 2 3 4 5 6 7 8 9 10 11 | @register.simple_tag
def time_of_day():
import datetime, pytz
from django.conf import settings
cur_time = datetime.datetime.now(tz=pytz.timezone(str(settings.TIME_ZONE)))
if cur_time.hour < 12:
return 'Morning'
elif 12 <= cur_time.hour < 18:
return 'Afternoon'
else:
return 'Evening'
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Please login first before commenting.