{"title":"Time zones","version":"6.0","locale":"en","docname":"topics/i18n/timezones","url":"/en/6.0/topics/i18n/timezones/","canonical":"https://djangodocs.dev/en/6.0/topics/i18n/timezones/","summary":"Overview Link to this heading # When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime…","html":"<h1>Time zones<a class=\"heading-anchor\" href=\"#time-zones\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<section id=\"overview\">\n<span id=\"time-zones-overview\"></span><h2>Overview<a class=\"heading-anchor\" href=\"#overview\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When support for time zones is enabled, Django stores datetime information in\nUTC in the database, uses time-zone-aware datetime objects internally, and\nconverts them to the end user’s time zone in forms. Templates will use the\n<a class=\"reference internal\" href=\"#default-current-time-zone\"><span class=\"std std-ref\">default time zone</span></a>, but this can be updated\nto the end user’s time zone through the use of <a class=\"reference internal\" href=\"#time-zones-in-templates\"><span class=\"std std-ref\">filters and tags</span></a>.</p>\n<p>This is handy if your users live in more than one time zone and you want to\ndisplay datetime information according to each user’s wall clock.</p>\n<p>Even if your website is available in only one time zone, it’s still good\npractice to store data in UTC in your database. The main reason is daylight\nsaving time (DST). Many countries have a system of DST, where clocks are moved\nforward in spring and backward in autumn. If you’re working in local time,\nyou’re likely to encounter errors twice a year, when the transitions happen.\nThis probably doesn’t matter for your blog, but it’s a problem if you over bill\nor under bill your customers by one hour, twice a year, every year. The\nsolution to this problem is to use UTC in the code and use local time only when\ninteracting with end users.</p>\n<p>Time zone support is enabled by default. To disable it, set <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span>\n<span class=\"pre\">False</span></code></a> in your settings file.</p>\n<p>Time zone support uses <a class=\"reference external\" href=\"https://docs.python.org/3/library/zoneinfo.html#module-zoneinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">zoneinfo</span></code></a>, which is part of the Python standard\nlibrary from Python 3.9.</p>\n<p>If you’re wrestling with a particular problem, start with the <a class=\"reference internal\" href=\"#time-zones-faq\"><span class=\"std std-ref\">time zone\nFAQ</span></a>.</p>\n</section>\n<section id=\"concepts\">\n<h2>Concepts<a class=\"heading-anchor\" href=\"#concepts\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"naive-and-aware-datetime-objects\">\n<span id=\"naive-vs-aware-datetimes\"></span><h3>Naive and aware datetime objects<a class=\"heading-anchor\" href=\"#naive-and-aware-datetime-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime.datetime</span></code></a> objects have a <code class=\"docutils literal notranslate\"><span class=\"pre\">tzinfo</span></code> attribute that\ncan be used to store time zone information, represented as an instance of a\nsubclass of <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.tzinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime.tzinfo</span></code></a>. When this attribute is set and describes\nan offset, a datetime object is <strong>aware</strong>. Otherwise, it’s <strong>naive</strong>.</p>\n<p>You can use <a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.is_aware\" title=\"django.utils.timezone.is_aware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">is_aware()</span></code></a> and\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.is_naive\" title=\"django.utils.timezone.is_naive\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">is_naive()</span></code></a> to determine whether datetimes are\naware or naive.</p>\n<p>When time zone support is disabled, Django uses naive datetime objects in local\ntime. This is sufficient for many use cases. In this mode, to obtain the\ncurrent time, you would write:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n\n<span class=\"n\">now</span> <span class=\"o\">=</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>When time zone support is enabled (<a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ=True</span></code></a>), Django\nuses time-zone-aware datetime objects. If your code creates datetime objects,\nthey should be aware too. In this mode, the example above becomes:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n\n<span class=\"n\">now</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span>\n</code></pre></div>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Dealing with aware datetime objects isn’t always intuitive. For instance,\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">tzinfo</span></code> argument of the standard datetime constructor doesn’t work\nreliably for time zones with DST. Using UTC is generally safe; if you’re\nusing other time zones, you should review the <a class=\"reference external\" href=\"https://docs.python.org/3/library/zoneinfo.html#module-zoneinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">zoneinfo</span></code></a>\ndocumentation carefully.</p>\n</aside>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>Python’s <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.time\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime.time</span></code></a> objects also feature a <code class=\"docutils literal notranslate\"><span class=\"pre\">tzinfo</span></code>\nattribute, and PostgreSQL has a matching <code class=\"docutils literal notranslate\"><span class=\"pre\">time</span> <span class=\"pre\">with</span> <span class=\"pre\">time</span> <span class=\"pre\">zone</span></code> type.\nHowever, as PostgreSQL’s docs put it, this type “exhibits properties which\nlead to questionable usefulness”.</p>\n<p>Django only supports naive time objects and will raise an exception if you\nattempt to save an aware time object, as a timezone for a time with no\nassociated date does not make sense.</p>\n</aside>\n</section>\n<section id=\"interpretation-of-naive-datetime-objects\">\n<span id=\"naive-datetime-objects\"></span><h3>Interpretation of naive datetime objects<a class=\"heading-anchor\" href=\"#interpretation-of-naive-datetime-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, Django still accepts naive datetime\nobjects, in order to preserve backwards-compatibility. When the database layer\nreceives one, it attempts to make it aware by interpreting it in the\n<a class=\"reference internal\" href=\"#default-current-time-zone\"><span class=\"std std-ref\">default time zone</span></a> and raises a warning.</p>\n<p>Unfortunately, during DST transitions, some datetimes don’t exist or are\nambiguous. That’s why you should always create aware datetime objects when time\nzone support is enabled. (See the <a class=\"reference external\" href=\"https://docs.python.org/3/library/zoneinfo.html#module-zoneinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">Using</span> <span class=\"pre\">ZoneInfo</span> <span class=\"pre\">section</span> <span class=\"pre\">of</span> <span class=\"pre\">the</span> <span class=\"pre\">zoneinfo</span>\n<span class=\"pre\">docs</span></code></a> for examples using the <code class=\"docutils literal notranslate\"><span class=\"pre\">fold</span></code> attribute to specify the\noffset that should apply to a datetime during a DST transition.)</p>\n<p>In practice, this is rarely an issue. Django gives you aware datetime objects\nin the models and forms, and most often, new datetime objects are created from\nexisting ones through <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.timedelta\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">timedelta</span></code></a> arithmetic. The only\ndatetime that’s often created in application code is the current time, and\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.now\" title=\"django.utils.timezone.now\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">timezone.now()</span></code></a> automatically does the\nright thing.</p>\n</section>\n<section id=\"default-time-zone-and-current-time-zone\">\n<span id=\"default-current-time-zone\"></span><h3>Default time zone and current time zone<a class=\"heading-anchor\" href=\"#default-time-zone-and-current-time-zone\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <strong>default time zone</strong> is the time zone defined by the <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a>\nsetting.</p>\n<p>The <strong>current time zone</strong> is the time zone that’s used for rendering.</p>\n<p>You should set the current time zone to the end user’s actual time zone with\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.activate\" title=\"django.utils.timezone.activate\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">activate()</span></code></a>. Otherwise, the default time zone is\nused.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>As explained in the documentation of <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a>, Django sets\nenvironment variables so that its process runs in the default time zone.\nThis happens regardless of the value of <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> and of the\ncurrent time zone.</p>\n<p>When <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>, this is useful to preserve\nbackwards-compatibility with applications that still rely on local time.\nHowever, <a class=\"reference internal\" href=\"#naive-datetime-objects\"><span class=\"std std-ref\">as explained above</span></a>, this isn’t\nentirely reliable, and you should always work with aware datetimes in UTC\nin your own code. For instance, use\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime.fromtimestamp\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">fromtimestamp()</span></code></a> and set the <code class=\"docutils literal notranslate\"><span class=\"pre\">tz</span></code> parameter to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.UTC\" title=\"(in Python v3.14)\"><code class=\"xref py py-obj docutils literal notranslate\"><span class=\"pre\">datetime.UTC</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"selecting-the-current-time-zone\">\n<h3>Selecting the current time zone<a class=\"heading-anchor\" href=\"#selecting-the-current-time-zone\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The current time zone is the equivalent of the current <a class=\"reference internal\" href=\"/en/6.0/topics/i18n/#term-locale-name\"><span class=\"xref std std-term\">locale</span></a> for translations. However, there’s no equivalent of the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Accept-Language</span></code> HTTP header that Django could use to determine the user’s\ntime zone automatically. Instead, Django provides <a class=\"reference internal\" href=\"/en/6.0/ref/utils/#time-zone-selection-functions\"><span class=\"std std-ref\">time zone selection\nfunctions</span></a>. Use them to build the time zone\nselection logic that makes sense for you.</p>\n<p>Most websites that care about time zones ask users in which time zone they live\nand store this information in the user’s profile. For anonymous users, they use\nthe time zone of their primary audience or UTC.\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/zoneinfo.html#zoneinfo.available_timezones\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">zoneinfo.available_timezones()</span></code></a> provides a set of available timezones that\nyou can use to build a map from likely locations to time zones.</p>\n<p>Here’s an example that stores the current timezone in the session. (It skips\nerror handling entirely for the sake of simplicity.)</p>\n<p>Add the following middleware to <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">zoneinfo</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">TimezoneMiddleware</span><span class=\"p\">:</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">get_response</span><span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">get_response</span> <span class=\"o\">=</span> <span class=\"n\">get_response</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__call__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">request</span><span class=\"p\">):</span>\n        <span class=\"n\">tzname</span> <span class=\"o\">=</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">session</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"s2\">&quot;django_timezone&quot;</span><span class=\"p\">)</span>\n        <span class=\"k\">if</span> <span class=\"n\">tzname</span><span class=\"p\">:</span>\n            <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">activate</span><span class=\"p\">(</span><span class=\"n\">zoneinfo</span><span class=\"o\">.</span><span class=\"n\">ZoneInfo</span><span class=\"p\">(</span><span class=\"n\">tzname</span><span class=\"p\">))</span>\n        <span class=\"k\">else</span><span class=\"p\">:</span>\n            <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">deactivate</span><span class=\"p\">()</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">get_response</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>Create a view that can set the current timezone:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">redirect</span><span class=\"p\">,</span> <span class=\"n\">render</span>\n\n<span class=\"c1\"># Prepare a map of common locations to timezone choices you wish to offer.</span>\n<span class=\"n\">common_timezones</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"s2\">&quot;London&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Europe/London&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;Paris&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;Europe/Paris&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;New York&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;America/New_York&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">}</span>\n\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">set_timezone</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">if</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">method</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;POST&quot;</span><span class=\"p\">:</span>\n        <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">session</span><span class=\"p\">[</span><span class=\"s2\">&quot;django_timezone&quot;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"n\">request</span><span class=\"o\">.</span><span class=\"n\">POST</span><span class=\"p\">[</span><span class=\"s2\">&quot;timezone&quot;</span><span class=\"p\">]</span>\n        <span class=\"k\">return</span> <span class=\"n\">redirect</span><span class=\"p\">(</span><span class=\"s2\">&quot;/&quot;</span><span class=\"p\">)</span>\n    <span class=\"k\">else</span><span class=\"p\">:</span>\n        <span class=\"k\">return</span> <span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"s2\">&quot;template.html&quot;</span><span class=\"p\">,</span> <span class=\"p\">{</span><span class=\"s2\">&quot;timezones&quot;</span><span class=\"p\">:</span> <span class=\"n\">common_timezones</span><span class=\"p\">})</span>\n</code></pre></div>\n<p>Include a form in <code class=\"docutils literal notranslate\"><span class=\"pre\">template.html</span></code> that will <code class=\"docutils literal notranslate\"><span class=\"pre\">POST</span></code> to this view:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">get_current_timezone</span> <span class=\"k\">as</span> <span class=\"nv\">TIME_ZONE</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">form</span> <span class=\"na\">action</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">url</span> <span class=\"s1\">&#39;set_timezone&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"na\">method</span><span class=\"o\">=</span><span class=\"s\">&quot;POST&quot;</span><span class=\"p\">&gt;</span>\n    <span class=\"cp\">{%</span> <span class=\"k\">csrf_token</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">label</span> <span class=\"na\">for</span><span class=\"o\">=</span><span class=\"s\">&quot;timezone&quot;</span><span class=\"p\">&gt;</span>Time zone:<span class=\"p\">&lt;/</span><span class=\"nt\">label</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">select</span> <span class=\"na\">name</span><span class=\"o\">=</span><span class=\"s\">&quot;timezone&quot;</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">for</span> <span class=\"nv\">city</span><span class=\"o\">,</span> <span class=\"nv\">tz</span> <span class=\"k\">in</span> <span class=\"nv\">timezones.items</span> <span class=\"cp\">%}</span>\n        <span class=\"p\">&lt;</span><span class=\"nt\">option</span> <span class=\"na\">value</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{{</span> <span class=\"nv\">tz</span> <span class=\"cp\">}}</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">if</span> <span class=\"nv\">tz</span> <span class=\"o\">==</span> <span class=\"nv\">TIME_ZONE</span> <span class=\"cp\">%}</span> <span class=\"na\">selected</span><span class=\"cp\">{%</span> <span class=\"k\">endif</span> <span class=\"cp\">%}</span><span class=\"p\">&gt;</span><span class=\"cp\">{{</span> <span class=\"nv\">city</span> <span class=\"cp\">}}</span><span class=\"p\">&lt;/</span><span class=\"nt\">option</span><span class=\"p\">&gt;</span>\n        <span class=\"cp\">{%</span> <span class=\"k\">endfor</span> <span class=\"cp\">%}</span>\n    <span class=\"p\">&lt;/</span><span class=\"nt\">select</span><span class=\"p\">&gt;</span>\n    <span class=\"p\">&lt;</span><span class=\"nt\">input</span> <span class=\"na\">type</span><span class=\"o\">=</span><span class=\"s\">&quot;submit&quot;</span> <span class=\"na\">value</span><span class=\"o\">=</span><span class=\"s\">&quot;Set&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">form</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"time-zone-aware-input-in-forms\">\n<span id=\"time-zones-in-forms\"></span><h2>Time zone aware input in forms<a class=\"heading-anchor\" href=\"#time-zone-aware-input-in-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When you enable time zone support, Django interprets datetimes entered in\nforms in the <a class=\"reference internal\" href=\"#default-current-time-zone\"><span class=\"std std-ref\">current time zone</span></a> and returns\naware datetime objects in <code class=\"docutils literal notranslate\"><span class=\"pre\">cleaned_data</span></code>.</p>\n<p>Converted datetimes that don’t exist or are ambiguous because they fall in a\nDST transition will be reported as invalid values.</p>\n</section>\n<section id=\"time-zone-aware-output-in-templates\">\n<span id=\"time-zones-in-templates\"></span><h2>Time zone aware output in templates<a class=\"heading-anchor\" href=\"#time-zone-aware-output-in-templates\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When you enable time zone support, Django converts aware datetime objects to\nthe <a class=\"reference internal\" href=\"#default-current-time-zone\"><span class=\"std std-ref\">current time zone</span></a> when they’re rendered\nin templates. This behaves very much like <a class=\"reference internal\" href=\"/en/6.0/topics/i18n/formatting/\"><span class=\"doc\">format localization</span></a>.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Django doesn’t convert naive datetime objects, because they could be\nambiguous, and because your code should never produce naive datetimes when\ntime zone support is enabled. However, you can force conversion with the\ntemplate filters described below.</p>\n</aside>\n<p>Conversion to local time isn’t always appropriate – you may be generating\noutput for computers rather than for humans. The following filters and tags,\nprovided by the <code class=\"docutils literal notranslate\"><span class=\"pre\">tz</span></code> template tag library, allow you to control the time zone\nconversions.</p>\n<section id=\"template-tags\">\n<h3>Template tags<a class=\"heading-anchor\" href=\"#template-tags\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"localtime\">\n<span id=\"std-templatetag-localtime\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localtime</span></code><a class=\"heading-anchor\" href=\"#localtime\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Enables or disables conversion of aware datetime objects to the current time\nzone in the contained block.</p>\n<p>This tag has exactly the same effects as the <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> setting as far\nas the template engine is concerned. It allows a more fine grained control of\nconversion.</p>\n<p>To activate or deactivate conversion for a template block, use:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localtime</span> <span class=\"nv\">on</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocaltime</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">localtime</span> <span class=\"nv\">off</span> <span class=\"cp\">%}</span>\n    <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endlocaltime</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>The value of <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> isn’t respected inside of a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">{%</span> <span class=\"pre\">localtime</span> <span class=\"pre\">%}</span></code> block.</p>\n</aside>\n</section>\n<section id=\"timezone\">\n<span id=\"std-templatetag-timezone\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">timezone</span></code><a class=\"heading-anchor\" href=\"#timezone\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Sets or unsets the current time zone in the contained block. When the current\ntime zone is unset, the default time zone applies.</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">timezone</span> <span class=\"s2\">&quot;Europe/Paris&quot;</span> <span class=\"cp\">%}</span>\n    Paris time: <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endtimezone</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{%</span> <span class=\"k\">timezone</span> <span class=\"kp\">None</span> <span class=\"cp\">%}</span>\n    Server time: <span class=\"cp\">{{</span> <span class=\"nv\">value</span> <span class=\"cp\">}}</span>\n<span class=\"cp\">{%</span> <span class=\"k\">endtimezone</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n</section>\n<section id=\"get-current-timezone\">\n<span id=\"std-templatetag-get_current_timezone\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">get_current_timezone</span></code><a class=\"heading-anchor\" href=\"#get-current-timezone\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>You can get the name of the current time zone using the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_current_timezone</span></code> tag:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">get_current_timezone</span> <span class=\"k\">as</span> <span class=\"nv\">TIME_ZONE</span> <span class=\"cp\">%}</span>\n</code></pre></div>\n<p>Alternatively, you can activate the\n<a class=\"reference internal\" href=\"/en/6.0/ref/templates/api/#django.template.context_processors.tz\" title=\"django.template.context_processors.tz\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">tz()</span></code></a> context processor and\nuse the <code class=\"docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code> context variable.</p>\n</section>\n</section>\n<section id=\"template-filters\">\n<h3>Template filters<a class=\"heading-anchor\" href=\"#template-filters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>These filters accept both aware and naive datetimes. For conversion purposes,\nthey assume that naive datetimes are in the default time zone. They always\nreturn aware datetimes.</p>\n<section id=\"std-templatefilter-localtime\">\n<span id=\"id1\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">localtime</span></code><a class=\"heading-anchor\" href=\"#std-templatefilter-localtime\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Forces conversion of a single value to the current time zone.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">localtime</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n<section id=\"utc\">\n<span id=\"std-templatefilter-utc\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">utc</span></code><a class=\"heading-anchor\" href=\"#utc\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Forces conversion of a single value to UTC.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">utc</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n<section id=\"std-templatefilter-timezone\">\n<span id=\"id2\"></span><h4><code class=\"docutils literal notranslate\"><span class=\"pre\">timezone</span></code><a class=\"heading-anchor\" href=\"#std-templatefilter-timezone\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Forces conversion of a single value to an arbitrary timezone.</p>\n<p>The argument must be an instance of a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.tzinfo\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">tzinfo</span></code></a> subclass or a\ntime zone name.</p>\n<p>For example:</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">tz</span> <span class=\"cp\">%}</span>\n\n<span class=\"cp\">{{</span> <span class=\"nv\">value</span><span class=\"o\">|</span><span class=\"nf\">timezone</span><span class=\"s2\">:&quot;Europe/Paris&quot;</span> <span class=\"cp\">}}</span>\n</code></pre></div>\n</section>\n</section>\n</section>\n<section id=\"migration-guide\">\n<span id=\"time-zones-migration-guide\"></span><h2>Migration guide<a class=\"heading-anchor\" href=\"#migration-guide\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Here’s how to migrate a project that was started before Django supported time\nzones.</p>\n<section id=\"database\">\n<h3>Database<a class=\"heading-anchor\" href=\"#database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<section id=\"postgresql\">\n<h4>PostgreSQL<a class=\"heading-anchor\" href=\"#postgresql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>The PostgreSQL backend stores datetimes as <code class=\"docutils literal notranslate\"><span class=\"pre\">timestamp</span> <span class=\"pre\">with</span> <span class=\"pre\">time</span> <span class=\"pre\">zone</span></code>. In\npractice, this means it converts datetimes from the connection’s time zone to\nUTC on storage, and from UTC to the connection’s time zone on retrieval.</p>\n<p>As a consequence, if you’re using PostgreSQL, you can switch between <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span>\n<span class=\"pre\">=</span> <span class=\"pre\">False</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code> freely. The database connection’s time zone\nwill be set to <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-DATABASE-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASE-TIME_ZONE</span></code></a> or <code class=\"docutils literal notranslate\"><span class=\"pre\">UTC</span></code> respectively, so that\nDjango obtains correct datetimes in all cases. You don’t need to perform any\ndata conversions.</p>\n<aside class=\"admonition-time-zone-settings admonition\">\n<p class=\"admonition-title\">Time zone settings</p>\n<p>The <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-DATABASE-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">time</span> <span class=\"pre\">zone</span></code></a> configured for the connection\nin the <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> setting is distinct from the general\n<a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a> setting.</p>\n</aside>\n</section>\n<section id=\"other-databases\">\n<h4>Other databases<a class=\"heading-anchor\" href=\"#other-databases\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<p>Other backends store datetimes without time zone information. If you switch\nfrom <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">False</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code>, you must convert your data from\nlocal time to UTC – which isn’t deterministic if your local time has DST.</p>\n</section>\n</section>\n<section id=\"code\">\n<h3>Code<a class=\"heading-anchor\" href=\"#code\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The first step is to add <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code></a> to your settings\nfile. At this point, things should mostly work. If you create naive datetime\nobjects in your code, Django makes them aware when necessary.</p>\n<p>However, these conversions may fail around DST transitions, which means you\naren’t getting the full benefits of time zone support yet. Also, you’re likely\nto run into a few problems because it’s impossible to compare a naive datetime\nwith an aware datetime. Since Django now gives you aware datetimes, you’ll get\nexceptions wherever you compare a datetime that comes from a model or a form\nwith a naive datetime that you’ve created in your code.</p>\n<p>So the second step is to refactor your code wherever you instantiate datetime\nobjects to make them aware. This can be done incrementally.\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#module-django.utils.timezone\" title=\"django.utils.timezone: Timezone support.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.timezone</span></code></a> defines some handy helpers for compatibility\ncode: <a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.now\" title=\"django.utils.timezone.now\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">now()</span></code></a>,\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.is_aware\" title=\"django.utils.timezone.is_aware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">is_aware()</span></code></a>,\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.is_naive\" title=\"django.utils.timezone.is_naive\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">is_naive()</span></code></a>,\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.make_aware\" title=\"django.utils.timezone.make_aware\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">make_aware()</span></code></a>, and\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.make_naive\" title=\"django.utils.timezone.make_naive\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">make_naive()</span></code></a>.</p>\n<p>Finally, in order to help you locate code that needs upgrading, Django raises\na warning when you attempt to save a naive datetime to the database:</p>\n<div class=\"code-block\" data-language=\"pytb\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Pytb</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Pytb code\"><code><span class=\"x\">RuntimeWarning: DateTimeField ModelName.field_name received a naive</span>\n<span class=\"x\">datetime (2012-01-01 00:00:00) while time zone support is active.</span>\n</code></pre></div>\n<p>During development, you can turn such warnings into exceptions and get a\ntraceback by adding the following to your settings file:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">warnings</span>\n\n<span class=\"n\">warnings</span><span class=\"o\">.</span><span class=\"n\">filterwarnings</span><span class=\"p\">(</span>\n    <span class=\"s2\">&quot;error&quot;</span><span class=\"p\">,</span>\n    <span class=\"sa\">r</span><span class=\"s2\">&quot;DateTimeField .* received a naive datetime&quot;</span><span class=\"p\">,</span>\n    <span class=\"ne\">RuntimeWarning</span><span class=\"p\">,</span>\n    <span class=\"sa\">r</span><span class=\"s2\">&quot;django\\.db\\.models\\.fields&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"fixtures\">\n<h3>Fixtures<a class=\"heading-anchor\" href=\"#fixtures\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When serializing an aware datetime, the UTC offset is included, like this:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"s2\">&quot;2011-09-01T13:20:30+03:00&quot;</span>\n</code></pre></div>\n<p>While for a naive datetime, it isn’t:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"s2\">&quot;2011-09-01T13:20:30&quot;</span>\n</code></pre></div>\n<p>For models with <a class=\"reference internal\" href=\"/en/6.0/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateTimeField</span></code></a>s, this difference\nmakes it impossible to write a fixture that works both with and without time\nzone support.</p>\n<p>Fixtures generated with <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">False</span></code>, or before Django 1.4, use the\n“naive” format. If your project contains such fixtures, after you enable time\nzone support, you’ll see <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#RuntimeWarning\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">RuntimeWarning</span></code></a>s when you load them. To get\nrid of the warnings, you must convert your fixtures to the “aware” format.</p>\n<p>You can regenerate fixtures with <a class=\"reference internal\" href=\"/en/6.0/ref/django-admin/#django-admin-loaddata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">loaddata</span></code></a> then <a class=\"reference internal\" href=\"/en/6.0/ref/django-admin/#django-admin-dumpdata\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">dumpdata</span></code></a>.\nOr, if they’re small enough, you can edit them to add the UTC offset that\nmatches your <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a> to each serialized datetime.</p>\n</section>\n</section>\n<section id=\"faq\">\n<span id=\"time-zones-faq\"></span><h2>FAQ<a class=\"heading-anchor\" href=\"#faq\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"setup\">\n<h3>Setup<a class=\"heading-anchor\" href=\"#setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<ol class=\"arabic\">\n<li><p><strong>I don’t need multiple time zones. Should I enable time zone support?</strong></p>\n<p>Yes. When time zone support is enabled, Django uses a more accurate model\nof local time. This shields you from subtle and unreproducible bugs around\ndaylight saving time (DST) transitions.</p>\n<p>When you enable time zone support, you’ll encounter some errors because\nyou’re using naive datetimes where Django expects aware datetimes. Such\nerrors show up when running tests. You’ll quickly learn how to avoid invalid\noperations.</p>\n<p>On the other hand, bugs caused by the lack of time zone support are much\nharder to prevent, diagnose and fix. Anything that involves scheduled tasks\nor datetime arithmetic is a candidate for subtle bugs that will bite you\nonly once or twice a year.</p>\n<p>For these reasons, time zone support is enabled by default in new projects,\nand you should keep it unless you have a very good reason not to.</p>\n</li>\n<li><p><strong>I’ve enabled time zone support. Am I safe?</strong></p>\n<p>Maybe. You’re better protected from DST-related bugs, but you can still\nshoot yourself in the foot by carelessly turning naive datetimes into aware\ndatetimes, and vice-versa.</p>\n<p>If your application connects to other systems – for instance, if it queries\na web service – make sure datetimes are properly specified. To transmit\ndatetimes safely, their representation should include the UTC offset, or\ntheir values should be in UTC (or both!).</p>\n<p>Finally, our calendar system contains interesting edge cases. For example,\nyou can’t always subtract one year directly from a given date:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">one_year_before</span><span class=\"p\">(</span><span class=\"n\">value</span><span class=\"p\">):</span>  <span class=\"c1\"># Wrong example.</span>\n<span class=\"gp\">... </span>    <span class=\"k\">return</span> <span class=\"n\">value</span><span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"n\">year</span><span class=\"o\">=</span><span class=\"n\">value</span><span class=\"o\">.</span><span class=\"n\">year</span> <span class=\"o\">-</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">one_year_before</span><span class=\"p\">(</span><span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">datetime</span><span class=\"p\">(</span><span class=\"mi\">2012</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">10</span><span class=\"p\">,</span> <span class=\"mi\">0</span><span class=\"p\">))</span>\n<span class=\"go\">datetime.datetime(2011, 3, 1, 10, 0)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">one_year_before</span><span class=\"p\">(</span><span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">datetime</span><span class=\"p\">(</span><span class=\"mi\">2012</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">29</span><span class=\"p\">,</span> <span class=\"mi\">10</span><span class=\"p\">,</span> <span class=\"mi\">0</span><span class=\"p\">))</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"c\">...</span>\n<span class=\"gr\">ValueError</span>: <span class=\"n\">day is out of range for month</span>\n</code></pre></div>\n<p>To implement such a function correctly, you must decide whether 2012-02-29\nminus one year is 2011-02-28 or 2011-03-01, which depends on your business\nrequirements.</p>\n</li>\n<li><p><strong>How do I interact with a database that stores datetimes in local time?</strong></p>\n<p>Set the <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-DATABASE-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a> option to the appropriate\ntime zone for this database in the <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> setting.</p>\n<p>This is useful for connecting to a database that doesn’t support time zones\nand that isn’t managed by Django when <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>.</p>\n</li>\n</ol>\n</section>\n<section id=\"troubleshooting\">\n<h3>Troubleshooting<a class=\"heading-anchor\" href=\"#troubleshooting\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<ol class=\"arabic\">\n<li><p><strong>My application crashes with</strong> <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError:</span> <span class=\"pre\">can't</span> <span class=\"pre\">compare</span> <span class=\"pre\">offset-naive</span></code>\n<code class=\"docutils literal notranslate\"><span class=\"pre\">and</span> <span class=\"pre\">offset-aware</span> <span class=\"pre\">datetimes</span></code> <strong>– what’s wrong?</strong></p>\n<p>Let’s reproduce this error by comparing a naive and an aware datetime:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">aware</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">naive</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">make_naive</span><span class=\"p\">(</span><span class=\"n\">aware</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">naive</span> <span class=\"o\">==</span> <span class=\"n\">aware</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"c\">...</span>\n<span class=\"gr\">TypeError</span>: <span class=\"n\">can&#39;t compare offset-naive and offset-aware datetimes</span>\n</code></pre></div>\n<p>If you encounter this error, most likely your code is comparing these two\nthings:</p>\n<ul class=\"simple\">\n<li><p>a datetime provided by Django – for instance, a value read from a form or\na model field. Since you enabled time zone support, it’s aware.</p></li>\n<li><p>a datetime generated by your code, which is naive (or you wouldn’t be\nreading this).</p></li>\n</ul>\n<p>Generally, the correct solution is to change your code to use an aware\ndatetime instead.</p>\n<p>If you’re writing a pluggable application that’s expected to work\nindependently of the value of <a class=\"reference internal\" href=\"/en/6.0/ref/settings/#std-setting-USE_TZ\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_TZ</span></code></a>, you may find\n<a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.now\" title=\"django.utils.timezone.now\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.timezone.now()</span></code></a> useful. This function returns the current\ndate and time as a naive datetime when <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">False</span></code> and as an aware\ndatetime when <code class=\"docutils literal notranslate\"><span class=\"pre\">USE_TZ</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code>. You can add or subtract\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.timedelta\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime.timedelta</span></code></a> as needed.</p>\n</li>\n<li><p><strong>I see lots of</strong> <code class=\"docutils literal notranslate\"><span class=\"pre\">RuntimeWarning:</span> <span class=\"pre\">DateTimeField</span> <span class=\"pre\">received</span> <span class=\"pre\">a</span> <span class=\"pre\">naive</span>\n<span class=\"pre\">datetime</span></code> <code class=\"docutils literal notranslate\"><span class=\"pre\">(YYYY-MM-DD</span> <span class=\"pre\">HH:MM:SS)</span></code> <code class=\"docutils literal notranslate\"><span class=\"pre\">while</span> <span class=\"pre\">time</span> <span class=\"pre\">zone</span> <span class=\"pre\">support</span> <span class=\"pre\">is</span> <span class=\"pre\">active</span></code>\n<strong>– is that bad?</strong></p>\n<p>When time zone support is enabled, the database layer expects to receive\nonly aware datetimes from your code. This warning occurs when it receives a\nnaive datetime. This indicates that you haven’t finished porting your code\nfor time zone support. Please refer to the <a class=\"reference internal\" href=\"#time-zones-migration-guide\"><span class=\"std std-ref\">migration guide</span></a> for tips on this process.</p>\n<p>In the meantime, for backwards compatibility, the datetime is considered to\nbe in the default time zone, which is generally what you expect.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">now.date()</span></code> <strong>is yesterday! (or tomorrow)</strong></p>\n<p>If you’ve always used naive datetimes, you probably believe that you can\nconvert a datetime to a date by calling its <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime.date\" title=\"(in Python v3.14)\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">date()</span></code></a>\nmethod. You also consider that a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.date\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">date</span></code></a> is a lot like a\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime</span></code></a>, except that it’s less accurate.</p>\n<p>None of this is true in a time zone aware environment:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">zoneinfo</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">paris_tz</span> <span class=\"o\">=</span> <span class=\"n\">zoneinfo</span><span class=\"o\">.</span><span class=\"n\">ZoneInfo</span><span class=\"p\">(</span><span class=\"s2\">&quot;Europe/Paris&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">new_york_tz</span> <span class=\"o\">=</span> <span class=\"n\">zoneinfo</span><span class=\"o\">.</span><span class=\"n\">ZoneInfo</span><span class=\"p\">(</span><span class=\"s2\">&quot;America/New_York&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">paris</span> <span class=\"o\">=</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">datetime</span><span class=\"p\">(</span><span class=\"mi\">2012</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">,</span> <span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">30</span><span class=\"p\">,</span> <span class=\"n\">tzinfo</span><span class=\"o\">=</span><span class=\"n\">paris_tz</span><span class=\"p\">)</span>\n<span class=\"go\"># This is the correct way to convert between time zones.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">new_york</span> <span class=\"o\">=</span> <span class=\"n\">paris</span><span class=\"o\">.</span><span class=\"n\">astimezone</span><span class=\"p\">(</span><span class=\"n\">new_york_tz</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">paris</span> <span class=\"o\">==</span> <span class=\"n\">new_york</span><span class=\"p\">,</span> <span class=\"n\">paris</span><span class=\"o\">.</span><span class=\"n\">date</span><span class=\"p\">()</span> <span class=\"o\">==</span> <span class=\"n\">new_york</span><span class=\"o\">.</span><span class=\"n\">date</span><span class=\"p\">()</span>\n<span class=\"go\">(True, False)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">paris</span> <span class=\"o\">-</span> <span class=\"n\">new_york</span><span class=\"p\">,</span> <span class=\"n\">paris</span><span class=\"o\">.</span><span class=\"n\">date</span><span class=\"p\">()</span> <span class=\"o\">-</span> <span class=\"n\">new_york</span><span class=\"o\">.</span><span class=\"n\">date</span><span class=\"p\">()</span>\n<span class=\"go\">(datetime.timedelta(0), datetime.timedelta(1))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">paris</span>\n<span class=\"go\">datetime.datetime(2012, 3, 3, 1, 30, tzinfo=zoneinfo.ZoneInfo(key=&#39;Europe/Paris&#39;))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">new_york</span>\n<span class=\"go\">datetime.datetime(2012, 3, 2, 19, 30, tzinfo=zoneinfo.ZoneInfo(key=&#39;America/New_York&#39;))</span>\n</code></pre></div>\n<p>As this example shows, the same datetime has a different date, depending on\nthe time zone in which it is represented. But the real problem is more\nfundamental.</p>\n<p>A datetime represents a <strong>point in time</strong>. It’s absolute: it doesn’t depend\non anything. On the contrary, a date is a <strong>calendaring concept</strong>. It’s a\nperiod of time whose bounds depend on the time zone in which the date is\nconsidered. As you can see, these two concepts are fundamentally different,\nand converting a datetime to a date isn’t a deterministic operation.</p>\n<p>What does this mean in practice?</p>\n<p>Generally, you should avoid converting a <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">datetime</span></code></a> to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#datetime.date\" title=\"(in Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">date</span></code></a>. For instance, you can use the <a class=\"reference internal\" href=\"/en/6.0/ref/templates/builtins/#std-templatefilter-date\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">date</span></code></a>\ntemplate filter to only show the date part of a datetime. This filter will\nconvert the datetime into the current time zone before formatting it,\nensuring the results appear correctly.</p>\n<p>If you really need to do the conversion yourself, you must ensure the\ndatetime is converted to the appropriate time zone first. Usually, this\nwill be the current timezone:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">activate</span><span class=\"p\">(</span><span class=\"n\">zoneinfo</span><span class=\"o\">.</span><span class=\"n\">ZoneInfo</span><span class=\"p\">(</span><span class=\"s2\">&quot;Asia/Singapore&quot;</span><span class=\"p\">))</span>\n<span class=\"go\"># For this example, we set the time zone to Singapore, but here&#39;s how</span>\n<span class=\"go\"># you would obtain the current time zone in the general case.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">current_tz</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">get_current_timezone</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">local</span> <span class=\"o\">=</span> <span class=\"n\">paris</span><span class=\"o\">.</span><span class=\"n\">astimezone</span><span class=\"p\">(</span><span class=\"n\">current_tz</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">local</span>\n<span class=\"go\">datetime.datetime(2012, 3, 3, 8, 30, tzinfo=zoneinfo.ZoneInfo(key=&#39;Asia/Singapore&#39;))</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">local</span><span class=\"o\">.</span><span class=\"n\">date</span><span class=\"p\">()</span>\n<span class=\"go\">datetime.date(2012, 3, 3)</span>\n</code></pre></div>\n</li>\n<li><p><strong>I get an error</strong> “<code class=\"docutils literal notranslate\"><span class=\"pre\">Are</span> <span class=\"pre\">time</span> <span class=\"pre\">zone</span> <span class=\"pre\">definitions</span> <span class=\"pre\">for</span> <span class=\"pre\">your</span> <span class=\"pre\">database</span>\n<span class=\"pre\">installed?</span></code>”</p>\n<p>If you are using MySQL, see the <a class=\"reference internal\" href=\"/en/6.0/ref/databases/#mysql-time-zone-definitions\"><span class=\"std std-ref\">Time zone definitions</span></a> section\nof the MySQL notes for instructions on loading time zone definitions.</p>\n</li>\n</ol>\n</section>\n<section id=\"usage\">\n<h3>Usage<a class=\"heading-anchor\" href=\"#usage\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<ol class=\"arabic\">\n<li><p><strong>I have a string</strong> <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;2012-02-21</span> <span class=\"pre\">10:28:45&quot;</span></code> <strong>and I know it’s in the</strong>\n<code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;Europe/Helsinki&quot;</span></code> <strong>time zone. How do I turn that into an aware\ndatetime?</strong></p>\n<p>Here you need to create the required <code class=\"docutils literal notranslate\"><span class=\"pre\">ZoneInfo</span></code> instance and attach it to\nthe naïve datetime:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">zoneinfo</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.dateparse</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">parse_datetime</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">naive</span> <span class=\"o\">=</span> <span class=\"n\">parse_datetime</span><span class=\"p\">(</span><span class=\"s2\">&quot;2012-02-21 10:28:45&quot;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">naive</span><span class=\"o\">.</span><span class=\"n\">replace</span><span class=\"p\">(</span><span class=\"n\">tzinfo</span><span class=\"o\">=</span><span class=\"n\">zoneinfo</span><span class=\"o\">.</span><span class=\"n\">ZoneInfo</span><span class=\"p\">(</span><span class=\"s2\">&quot;Europe/Helsinki&quot;</span><span class=\"p\">))</span>\n<span class=\"go\">datetime.datetime(2012, 2, 21, 10, 28, 45, tzinfo=zoneinfo.ZoneInfo(key=&#39;Europe/Helsinki&#39;))</span>\n</code></pre></div>\n</li>\n<li><p><strong>How can I obtain the local time in the current time zone?</strong></p>\n<p>Well, the first question is, do you really need to?</p>\n<p>You should only use local time when you’re interacting with humans, and the\ntemplate layer provides <a class=\"reference internal\" href=\"#time-zones-in-templates\"><span class=\"std std-ref\">filters and tags</span></a>\nto convert datetimes to the time zone of your choice.</p>\n<p>Furthermore, Python knows how to compare aware datetimes, taking into\naccount UTC offsets when necessary. It’s much easier (and possibly faster)\nto write all your model and view code in UTC. So, in most circumstances,\nthe datetime in UTC returned by <a class=\"reference internal\" href=\"/en/6.0/ref/utils/#django.utils.timezone.now\" title=\"django.utils.timezone.now\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.utils.timezone.now()</span></code></a> will be\nsufficient.</p>\n<p>For the sake of completeness, though, if you really want the local time\nin the current time zone, here’s how you can obtain it:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">localtime</span><span class=\"p\">(</span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">())</span>\n<span class=\"go\">datetime.datetime(2012, 3, 3, 20, 10, 53, 873365, tzinfo=zoneinfo.ZoneInfo(key=&#39;Europe/Paris&#39;))</span>\n</code></pre></div>\n<p>In this example, the current time zone is <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;Europe/Paris&quot;</span></code>.</p>\n</li>\n<li><p><strong>How can I see all available time zones?</strong></p>\n<p><a class=\"reference external\" href=\"https://docs.python.org/3/library/zoneinfo.html#zoneinfo.available_timezones\" title=\"(in Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">zoneinfo.available_timezones()</span></code></a> provides the set of all valid keys for\nIANA time zones available to your system. See the docs for usage\nconsiderations.</p>\n</li>\n</ol>\n</section>\n</section>","rootId":"time-zones","toc":[{"title":"Overview","anchor":"overview","children":[]},{"title":"Concepts","anchor":"concepts","children":[{"title":"Naive and aware datetime objects","anchor":"naive-and-aware-datetime-objects","children":[]},{"title":"Interpretation of naive datetime objects","anchor":"interpretation-of-naive-datetime-objects","children":[]},{"title":"Default time zone and current time zone","anchor":"default-time-zone-and-current-time-zone","children":[]},{"title":"Selecting the current time zone","anchor":"selecting-the-current-time-zone","children":[]}]},{"title":"Time zone aware input in forms","anchor":"time-zone-aware-input-in-forms","children":[]},{"title":"Time zone aware output in templates","anchor":"time-zone-aware-output-in-templates","children":[{"title":"Template tags","anchor":"template-tags","children":[{"title":"localtime","anchor":"localtime","children":[]},{"title":"timezone","anchor":"timezone","children":[]},{"title":"get_current_timezone","anchor":"get-current-timezone","children":[]}]},{"title":"Template filters","anchor":"template-filters","children":[{"title":"localtime","anchor":"std-templatefilter-localtime","children":[]},{"title":"utc","anchor":"utc","children":[]},{"title":"timezone","anchor":"std-templatefilter-timezone","children":[]}]}]},{"title":"Migration guide","anchor":"migration-guide","children":[{"title":"Database","anchor":"database","children":[{"title":"PostgreSQL","anchor":"postgresql","children":[]},{"title":"Other databases","anchor":"other-databases","children":[]}]},{"title":"Code","anchor":"code","children":[]},{"title":"Fixtures","anchor":"fixtures","children":[]}]},{"title":"FAQ","anchor":"faq","children":[{"title":"Setup","anchor":"setup","children":[]},{"title":"Troubleshooting","anchor":"troubleshooting","children":[]},{"title":"Usage","anchor":"usage","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/en/6.0/topics/"},{"docname":"topics/i18n/index","title":"Internationalization and localization","url":"/en/6.0/topics/i18n/"}],"prev":{"docname":"topics/i18n/formatting","title":"Format localization","url":"/en/6.0/topics/i18n/formatting/"},"next":{"docname":"topics/logging","title":"Logging","url":"/en/6.0/topics/logging/"},"formats":{"html":"/en/6.0/topics/i18n/timezones/","markdown":"/en/6.0/topics/i18n/timezones.md","json":"/en/6.0/topics/i18n/timezones.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/topics/i18n/timezones.txt","official":"https://docs.djangoproject.com/en/6.0/topics/i18n/timezones/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}