Js Date对象到python datetime

时间:2011-11-16 14:53:32

标签: javascript python parsing datetime date

我正在使用dhtmlxscheduler,我正在向django服务器发送日期以进行处理。

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

选择其中一个toString方法然后使用datetime.strptime()在python服务器端解析它以创建python datetime对象的最简单方法是什么?

简单的toString方法以格式返回日期时间:

Sat Nov 19 2011 00:00:00 GMT-0500 (EST)

尝试使用不同的格式指令证明是不成功的。

即:

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)

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'

3 个答案:

答案 0 :(得分:13)

toUTCString()给出:

"Tue, 22 Nov 2011 06:00:00 GMT"

这可以解决:

datetime.strptime("Tue, 22 Nov 2011 06:00:00 GMT", "%a, %d %b %Y %H:%M:%S %Z")

答案 1 :(得分:6)

看起来你得到的东西就像一个普通的javascript Date对象。在这种情况下,最简单的方法可能是使用getTime方法来获取时间戳。它应该返回类似1321463229215的内容,这只是一个以毫秒为单位的时间戳。

datetime的fromtimestamp需要一个以秒为单位的时间戳,所以只需将该时间戳除以1000.0就可以了,

from datetime import datetime
datetime.fromtimestamp(1321463229215 / 1000.0)

答案 2 :(得分:1)

尝试使用 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将是一个日期时间对象