javascript JS日期对象到python日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8153631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Js Date object to python datetime
提问by TheOne
I am working with dhtmlxscheduler and I am sending dates to the django server for processing.
我正在使用 dhtmlxscheduler 并将日期发送到 django 服务器进行处理。
Dhtmlxscheduler provides me with the following date object, the methods provided start from the second line below:
Dhtmlxscheduler 为我提供了以下日期对象,提供的方法从下面的第二行开始:
end_date: Sat Nov 19 2011 01:00:00 GMT-0500 (EST)
__proto__: Invalid Date
constructor: function Date() { [native code] }
getDate: function getDate() { [native code] }
getDay: function getDay() { [native code] }
getFullYear: function getFullYear() { [native code] }
getHours: function getHours() { [native code] }
getMilliseconds: function getMilliseconds() { [native code] }
getMinutes: function getMinutes() { [native code] }
getMonth: function getMonth() { [native code] }
getSeconds: function getSeconds() { [native code] }
getTime: function getTime() { [native code] }
getTimezoneOffset: function getTimezoneOffset() { [native code] }
getUTCDate: function getUTCDate() { [native code] }
getUTCDay: function getUTCDay() { [native code] }
getUTCFullYear: function getUTCFullYear() { [native code] }
getUTCHours: function getUTCHours() { [native code] }
getUTCMilliseconds: function getUTCMilliseconds() { [native code] }
getUTCMinutes: function getUTCMinutes() { [native code] }
getUTCMonth: function getUTCMonth() { [native code] }
getUTCSeconds: function getUTCSeconds() { [native code] }
getYear: function getYear() { [native code] }
setDate: function setDate() { [native code] }
setFullYear: function setFullYear() { [native code] }
setHours: function setHours() { [native code] }
setMilliseconds: function setMilliseconds() { [native code] }
setMinutes: function setMinutes() { [native code] }
setMonth: function setMonth() { [native code] }
setSeconds: function setSeconds() { [native code] }
setTime: function setTime() { [native code] }
setUTCDate: function setUTCDate() { [native code] }
setUTCFullYear: function setUTCFullYear() { [native code] }
setUTCHours: function setUTCHours() { [native code] }
setUTCMilliseconds: function setUTCMilliseconds() { [native code] }
setUTCMinutes: function setUTCMinutes() { [native code] }
setUTCMonth: function setUTCMonth() { [native code] }
setUTCSeconds: function setUTCSeconds() { [native code] }
setYear: function setYear() { [native code] }
toDateString: function toDateString() { [native code] }
toGMTString: function toGMTString() { [native code] }
toISOString: function toISOString() { [native code] }
toJSON: function toJSON() { [native code] }
toLocaleDateString: function toLocaleDateString() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toLocaleTimeString: function toLocaleTimeString() { [native code] }
toString: function toString() { [native code] }
toTimeString: function toTimeString() { [native code] }
toUTCString: function toUTCString() { [native code] }
valueOf: function valueOf() { [native code] }
__proto__: Object
What is the easiest method for choosing one of these toString methods and then parsing it on the python server side using datetime.strptime() to create a python datetime object?
选择这些 toString 方法之一然后在 python 服务器端使用 datetime.strptime() 解析它以创建 python datetime 对象的最简单方法是什么?
The simple toString method returns me a datetime in the format:
简单的 toString 方法以以下格式返回日期时间:
Sat Nov 19 2011 00:00:00 GMT-0500 (EST)
Trying the different format directives proves unsuccessful.
尝试不同的格式指令证明是不成功的。
ie:
IE:
datetime.strptime("Sat Nov 19 2011 00:00:00 GMT-0500 (EST)", "%a %b %d %Y %H:%M:%S %Z")
---> unconverted data remains: -0500 (EST)
and:
和:
datetime.strptime("Sat Nov 19 2011 00:00:00 GMT-0500 (EST)", "%a %b %d %Y %H:%M:%S %z")
---> ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S %z'
回答by TheOne
toUTCString() gives:
toUTCString() 给出:
"Tue, 22 Nov 2011 06:00:00 GMT"
And that's parsible with:
这可以解析为:
datetime.strptime("Tue, 22 Nov 2011 06:00:00 GMT", "%a, %d %b %Y %H:%M:%S %Z")
回答by NT3RP
It looks like you are getting something like an ordinary javascript Date object. In this case, the easiest method is probably to use the getTime
method to obtain a timestamp. It should return something like 1321463229215
, which is just a timestamp in milliseconds.
看起来你得到了一个普通的 javascript Date 对象。在这种情况下,最简单的方法可能是使用getTime
获取时间戳的方法。它应该返回类似1321463229215
,这只是一个以毫秒为单位的时间戳。
datetime's fromtimestamp
expects a timestamp in seconds, so just divide that timestamp by 1000.0 and you're good to go
datetimefromtimestamp
需要一个以秒为单位的时间戳,所以只需将该时间戳除以 1000.0 就可以了
from datetime import datetime
datetime.fromtimestamp(1321463229215 / 1000.0)
回答by Govind S Menokee
Try using python-dateutil(pip install python-dateutil)
尝试使用python-dateutil( pip install python-dateutil)
import dateutil.parser as dt
date_time_obj = dt.parse("Tue, 22 Nov 2011 06:00:00 GMT")
date_time_obj will be a datetime object
date_time_obj 将是一个日期时间对象