in settings.py I use:
TIME_ZONE = 'Europe/Moscow'
USE_I18N = True
USE_L10N = True
USE_TZ = True
On page.html:
{% now "jS F Y H:i" %}
And I see 25th December 2014 22:40, but now is 21:40. The same problem with auto_now_add=True in models fields - I have wrong time (hour+1).
How to fix that?
Thanks!
Related
I have this in the DB:
25.0|120|CashApp|0.0|0|1|2022-03-03 22:05:17.025000
This in the settings.py:
TIME_ZONE = 'US/Eastern' # = EST
USE_I18N = True
USE_L10N = True
USE_TZ = True
And this in the admin.py:
def timestamp_(self, obj):
return obj.timestamp.strftime("%d %b, %I:%M %p") # :%S
timestamp_.admin_order_field = 'timestamp'
And on the page it's displayed like this: 03 Mar, 10:05 PM
From what I understand it stores the timestamp in UTC in the DB, which is perfect, but why it displays it in UTC on the page? I've read that if I don't do activate(), the current timezone will be the same as default timezone, which should be EST in my case. What am I missing?
I am having difficulty getting queryset results in my own timezone.
for example:
model field definition:
some_datetime_field= models.DateTimeField(null=True)
Query:
MyModel.values_list("some_datetime_field",flat=True).first()
returns
datetime.datetime(2019, 11, 5, 14, 56, 16, tzinfo=<UTC>)
instead of returning
datetime.datetime(2019, 11, 5, 6, 56, 16, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
I am working with python V3.7.5, Django V2.2.7 and PostgreSQL V12 as my database.
In my setting I have:
TIME_ZONE = "America/Los_Angeles"
USE_TZ = True
USE_I18N = True
USE_L10N = True
From the documentation it says with regard to the "TIME_ZONE" settings: (https://docs.djangoproject.com/en/2.2/ref/settings/#time-zone)
When USE_TZ is True and the database supports time zones (e.g. PostgreSQL), it is an error to set this option.
so I tried to remove the TIME_ZONE from my setting:
USE_TZ = True
USE_I18N = True
USE_L10N = True
but that didnt work either, even worse, when I try to localize it with timezone.localtime(datetime_example) i get the time in Chicago time:
datetime.datetime(2019, 11, 5, 8, 56, 16, tzinfo=<DstTzInfo 'America/Chicago' CST-1 day, 18:00:00 STD>)
How can I get my query set to return DateTime in my chosen timezone instead of UTC?
The time that is being inserted in database is default UTC, not the timezone based time..I do not understand why is this happening, even though I am particularly specifying, in the query, the time that I want to be inserted.
In my Model I have
class leave(models.Model):
date_created = models.DateTimeField(auto_now_add=True)
In my settings I have
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
In my views i am setting timezone based on employee country
if employees.objects.get(emp_id=request.user.username).emp_loc == 'IND':
tzone=pytz.timezone('Asia/Calcutta')
elif employees.objects.get(emp_id=request.user.username).emp_loc == 'MLA':
tzone=pytz.timezone('Asia/Manila')
elif employees.objects.get(emp_id=request.user.username).emp_loc == 'MPLS':
tzone=pytz.timezone('CST6CDT')
And then I am creating leave and updating timezone based on country
new_leave = leave.objects.create(employee=employees.objects.get(emp_id = userid.emp_id), start_date=sdt, end_date=edt, ltype=ltyp, message=des,date_created=datetime.now(tzone))
new_leave.save()
Thanks in Advance.
You can create a middleware that handles the conversion of date and time,
import pytz
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
class TimezoneMiddleware(MiddlewareMixin):
def process_request(self, request):
tzname = request.session.get('django_timezone')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
and then you can as easily create the view with the conditions that you indicated earlier.
Best if you read the documentation as Kevin suggested.
I want to use django-datetime-widget in Polish language. But following instruction from project's github give me error. My situation is:
form:
class add_test_form(forms.Form):
test_date_finish = forms.DateTimeField(label="Termin zakonczenia testu", widget=DateTimeWidget(usel10n=True, bootstrap_version=3), localize=True)
view:
test = Test(test_date_start = request.POST['test_date_start'])
test.save()
model:
class Test(models.Model):
test_date_finish = models.DateTimeField()
settings.py:
LANGUAGE_CODE = 'pl-pl'
TIME_ZONE = 'Europe/Warsaw'
USE_I18N = True
USE_L10N = True
USE_TZ = True
I got ValidationError in Polish which translates into:
[...] value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[." "uuuuuu]][TZ] format.
Your date-time format isn't right, you should set it. This code in the project's docs may help you with that. Probably you should put this on your settings.py and change the format part according to your datetime format.
dateTimeOptions = {
'format': 'dd/mm/yyyy HH:ii P',
'autoclose': True,
'showMeridian' : True
}
widgets = {
#NOT Use localization and set a default format
'datetime': DateTimeWidget(options = dateTimeOptions)
}
I'm using Django 1.6 and I have to display a date. The timezone is Guayaquil (-05:00) and I have to get the date as:
{
'fecha': partido.fecha.strftime("%d %B"),
'hora': partido.fecha.strftime("%T"),
}
Expecting: '13 Junio' and '14:00:00' respectively, since that's the time saved and in Guayaquil timezone.
However what I get is the same time in UTC and months names in english: '13 June' and '19:00:00'.
What can I do to solve this issue?
You have to configure you time zone in your Django Settings
Choices can be found Here
Check in your settings.py:
TIME_ZONE = 'America/Guayaquil'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LANGUAGE_CODE = 'es'