Skip to content

Timezone Support

Gaurav Sharma edited this page Sep 18, 2026 · 2 revisions

Timezone support

mssql-django uses DATETIME2 for DateTimeField when USE_TZ=False and DATETIMEOFFSET when USE_TZ=True. If your application already has DateTimeField columns before enabling timezone support, migrate the existing DATETIME2 columns and convert their local values to UTC.

mssql-django 2.0 uses the standard-library zoneinfo module for named timezones. The tzdata package is installed automatically for systems without an IANA timezone database.

One migration approach is:

ALTER TABLE TABLENAME
   ALTER COLUMN DATETIMEFIELD DATETIMEOFFSET;

UPDATE TABLENAME
   SET DATETIMEFIELD = TODATETIMEOFFSET(DATETIMEFIELD, XXX) AT TIME ZONE 'UTC'

Replace TABLENAME with the table name, DATETIMEFIELD with the field's column name, and XXX with an integer offset in minutes or a string in {+|-}TZH:THM format. The supported range is -14 through +14 hours. Apply the migration to every existing DateTimeField column. A fixed offset does not account for historical daylight-saving transitions, so test the conversion against the source timezone before running it on production data.

For the remaining steps, follow Django's timezone migration guide.

Clone this wiki locally