【发布时间】:2016-09-04 07:21:23
【问题描述】:
当前的日期时间通过 ajax 请求 传递到 django 后端 并存储在数据库中。要将其存储在数据库中,必须首先将日期转换为 datetime 对象,这可以通过以下语句轻松地转换为 UTC 格式的日期(例如 Sun, 04 Sep 2016 07:13:06 GMT):
>>> from datetime import datetime
>>> datetime.strptime("Sun, 04 Sep 2016 07:13:06 GMT", "%a, %d %b %Y %H:%M:%S %Z")
但是,在这种方法中,不会保留用户的时区。
javascript Date 构造函数调用,即new Date() 返回以下格式的日期:
Sun Sep 04 2016 12:38:43 GMT+0530 (IST)
在转换为日期时间对象时会出错。
>>> datetime.strptime("Sun, 04 Sep 2016 07:13:06 GMT+0530 (IST)", "%a, %d %b %Y %H:%M:%S %Z")
ValueError: time data 'Sun Sep 04 2016 12:46:07 GMT+0530 (IST)' does not match format '%a, %d %b %Y %H:%M:%S %Z'
1) 如何解决这个问题? 2) 有没有更好的方法来解决它?
【问题讨论】:
标签: javascript python django datetime