I want to save the present date in database, but my Django server starting 13 hours earlier than the present time. Due to this the time is also changing when I use the following:
datetime.datetime.now()
I am using Python 2.7.5 with Django 1.5.4.
How can I display my present system date with django
..?
If your server date and time is wrong, try to fix that first. If they are correct, you can add a timezone value into your settings.py
:
USE_TZ = True
TIME_ZONE = 'UTC'
Check http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for a list of valid time zones.
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Kolkata'
USE_I18N = True
USE_L10N = True
USE_TZ = False
this is worked for me to set india timezone
There's a few options:
In settings.py
:
As one user commented, you technically can set TIME_ZONE = None
, and this will default to your physical computer time, this will be a real problem when trying to deploy a server or upload your project to a remote machine to serve it for you. For example, if you deployed your project on an amazon server with TIME_ZONE = None
, you may end up getting the system time of the physical machine hosting the server.
Instead, you must choose a valid tz
database time. If you look in the TZ
column on the following page: tz database timezones you may use any of these values.
Note: Manually setting a singular tz
database timezone may be limiting if your users are connecting from timezones other than yours (different parts of the country or world), or in timezones outside what you've manually set.
In this scenario, you either have to (1) have users registering supply their timezone, and alter your code, or (2) import django.utils timezone, see this page: django timezone module documentation. Another option is to (3) build a geolocator, which automatically grabs your users latitude and longitude position when they load your page and generates a timezone based on that information. Note that adjusting your timezone for users once you deploy your application is a more complicated process than setting a timezone for your local building and testing.
*//Answer edited to reflect a more holistic answer.