2

I have a Django API/PostgreSql project, where i try to store the datetime in UTC format, which later i will convert to a correct timezone in the front end with angular.

My current settings are:

USE_TZ = True
TIME_ZONE = 'US/Eastern'

I was first using UTC, but for some reason the datetime was stored as +1 hour, so i set the time zone to my servers time zone

Model:

date = models.DateTimeField(default = timezone.now())

But still when i retrieve the inserted record, the time is -3 minutes late.

While when i use the code bellow, it stores a correct time:

date = models.DateTimeField(auto_now_add=True)

Am i missing something in the configuration ? What TIME_ZONE should i use, server location time_zone, or it has to do with the servers time setup ?

user3334406
  • 337
  • 4
  • 16
  • Is your server's time off by 3 minutes? – Scott Marlowe Aug 16 '17 at 19:46
  • 1
    `date = models.DateTimeField(default = timezone.now())` <= This sets the default date to the time when the module is parsed. You probably wanted `default=timezone.now` (note missing parentheses). – dhke Aug 16 '17 at 19:56
  • Possible duplicate of [Django datetime issues (default=datetime.now())](https://stackoverflow.com/questions/2771676/django-datetime-issues-default-datetime-now) – dhke Aug 16 '17 at 19:56

1 Answers1

1

The issue is fixed, i dont know why but when im using:

date = models.DateTimeField(default = timezone.now()) # note: .now() with parenthesizes

The time gets stuck, at the time when the server started up.

The fix use:

date = models.DateTimeField(default = timezone.now) # without ()

Im on Django 1.6.5

user3334406
  • 337
  • 4
  • 16