【发布时间】:2021-11-29 19:57:12
【问题描述】:
如果我在 VS Code 中运行它,它会给我正确的本地时间:
from time import strftime
time = strftime("%H:%M %p")
print(time)
但如果我在 Django 项目中使用相同的代码,我的网站会显示比本地时间晚 8 小时的时间。
from time import strftime,localtime
def time_display(request):
context = {
"date":strftime("%b %d, %Y",localtime()), #DOESN'T DISPLAY LOCAL TIME
"time1": strftime("%H:%M %p",localtime()), #DOESN'T DISPLAY LOCAL TIME
"time3": strftime("%H:%M %p"), #DOESN'T DISPLAY LOCAL TIME
}
return render(request,'time_display.html',context)
如何修复它以显示正在查看网页的正确当地时间?
【问题讨论】:
-
localtime将返回服务器的本地时间。您应该获取浏览器的位置/时区并创建一个带有时区的时间对象:stackoverflow.com/questions/7065164/…
标签: python django time localtime