Js Date object to python datetime

2019-04-05 03:30发布

I am working with dhtmlxscheduler and I am sending dates to the django server for processing.

Dhtmlxscheduler provides me with the following date object, the methods provided start from the second line below:

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?

The simple toString method returns me a datetime in the format:

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

Trying the different format directives proves unsuccessful.

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'

3条回答
ゆ 、 Hurt°
2楼-- · 2019-04-05 03:34

Try using 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

查看更多
别忘想泡老子
3楼-- · 2019-04-05 03:40

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.

datetime's fromtimestamp expects a timestamp in seconds, so just divide that timestamp by 1000.0 and you're good to go

from datetime import datetime
datetime.fromtimestamp(1321463229215 / 1000.0)
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-04-05 03:53

toUTCString() gives:

"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")
查看更多
登录 后发表回答