16

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..?

anon582847382
  • 19,907
  • 5
  • 54
  • 57
Shiva
  • 988
  • 4
  • 15
  • 31

3 Answers3

23

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.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 4
    I used `UTC` as an example since I don't know your actual timezone. You may try `TIME_ZONE = 'Asia/Kolkata'` for +05:30. – Selcuk Mar 22 '14 at 13:54
  • 1
    Thank you,Thank you very much boss its working.Can we do this as default without changing settings(because if our project deployed at another country it should get its local time) – Shiva Mar 22 '14 at 14:03
  • 6
    In Windows, it is unreliable, but if you are using Unix you may try `TIME_ZONE = None` to use the operating system timezone. – Selcuk Mar 22 '14 at 14:05
  • 1
    You are super boss.Its working on my Linux.Thank you for reply – Shiva Mar 22 '14 at 14:09
10
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

Plutian
  • 2,276
  • 3
  • 14
  • 23
Akhil S
  • 955
  • 11
  • 16
0

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.

twknab
  • 1,741
  • 1
  • 21
  • 35