Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Js Date object to python datetime

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'
like image 513
TheOne Avatar asked Nov 16 '11 14:11

TheOne


People also ask

How do you convert a date object to a datetime object in Python?

Using the datetime() Pass the year, month and day values of the desired date object (as my_date. year, my_date. month, my_date. day) to this constructor to convert a date object to datetime object.

What is Strftime in Python?

The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation. Syntax : strftime(format) Returns : It returns the string representation of the date or time object.


1 Answers

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")
like image 88
TheOne Avatar answered Sep 20 '22 05:09

TheOne