17

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 Answers 3

25

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")
Sign up to request clarification or add additional context in comments.

1 Comment

if you expect aware datetime from "GMT" in the input, something like datetime.strptime("Tue, 22 Nov 2011 06:00:00 GMT".replace("GMT", "+00:00"), "%a, %d %b %Y %H:%M:%S %z") does the trick
15

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)

2 Comments

Unfortunately that method returns milliseconds units after seconds. ie: hour:minute:seconds:milleseconds.
Added an edit to NT3RP's post to change getUTCMilliseconds to getTime and divided the timestamp by 1000.0 since fromtimestamp expects a timestamp in seconds, not milliseconds.
9

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 Comments

This is surely the easiest option, but I think it's far slower than the others.
@NicholasObert In my experiment, this approach took about 130 µs and parsing with datetime took about 15 µs so this is almost 10 times slower. However, I think it's unlikely that this is a problem in a typical Python application.
Yes, it probably isn't going to make much difference, but it's still worth pointing out to whoever may opt for this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.