擅长:python、mysql、java
<p>来自<a href="https://docs.djangoproject.com/en/1.9/topics/i18n/timezones/" rel="nofollow">django docs</a>:</p>
<blockquote>
<p>How do I interact with a database that stores datetimes in local time?</p>
<p>Set the TIME_ZONE option to the appropriate time zone for this
database in the DATABASES setting.</p>
<p>This is useful for connecting to a database that doesn’t support time
zones and that isn’t managed by Django when USE_TZ is True.</p>
</blockquote>
<p>设置<code>USE_TZ=True</code>以便django在内部使用时区感知的datetime对象。</p>
<p>在<code>DATABASES</code>设置中设置<code>TIME_ZONE</code>,以容纳旧数据库。Django将自动将您的日期时间转换为相应的时区,即,要获得当前时间,它就足够了:</p>
<pre><code>from django.utils import timezone
now = timezone.now()
</code></pre>
<p>注意:<code>datetime.now()</code>返回一个原始的datetime对象。别用它。</p>