[Django]-TruncDate timezone parameter is not working in Django

10👍

You need to use TruncDay() instead of TruncDate. In the usage example just below that section of the documentation you’ll see the difference between the two.

TruncDate does not take a timezone option – it uses the current timezone and gives you the date in that timezone.

I think the distinction between the two is that TruncDate returns a DateField which by definition cannot be timezone-aware. TruncDay on the other hand returns a DateTimeField(with time portion set to 00:00:00), which can be timezone aware.

4👍

0👍

After Inspecting TruncDate Class looks like it doesn’t have timezone option

class TruncDate(TruncBase):
    kind = 'date'
    lookup_name = 'date'

    @cached_property
    def output_field(self):
        return DateField()

    def as_sql(self, compiler, connection):
        # Cast to date rather than truncate to date.
        lhs, lhs_params = compiler.compile(self.lhs)
        tzname = timezone.get_current_timezone_name() if settings.USE_TZ else None
        sql, tz_params = connection.ops.datetime_cast_date_sql(lhs, tzname)
        lhs_params.extend(tz_params)
        return sql, lhs_params

Leave a comment