Converting UTC datetime to user's local date and time

前端 未结 3 1366
天命终不由人
天命终不由人 2021-02-10 05:18

I\'m using python on Django and Google App Engine. I\'m also using the DateTimeProperty in one of my models. Occasionally I would like to display that date and time to the user.

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 06:00

    So I did a bit more research into this. To correctly do timezones, you have to follow something like what Tomasz described in his answer. However, if you're willing to tolerate a bit of kludge, you can get 90% of the way there with much less effort.

    When the user logs in or does any action on your site, you can use javascript to get their current timezone offset and then send it in your next request:

    var offset = (new Date()).getTimezoneOffset()
    $.post("post.html", "offset="offset.toString(), ... );
    

    Of course this assumes that the user has correctly set their current timezone. This is discussed on a number of other websites:

    • http://www.derickrethans.nl/detecting-timezone-by-ip.html
    • http://torrentialwebdev.com/blog/archives/152-Pre-populating-forms-with-the-timezone.html

提交回复
热议问题