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'
TheOne
  • 10,819
  • 20
  • 81
  • 119

3 Answers3

24

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")
TheOne
  • 10,819
  • 20
  • 81
  • 119
  • 1
    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 – FObersteiner Aug 05 '21 at 14:31
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)
Andrew
  • 1,890
  • 3
  • 16
  • 35
NT3RP
  • 15,262
  • 9
  • 61
  • 97
  • 2
    Unfortunately that method returns milliseconds units after seconds. ie: hour:minute:seconds:milleseconds. – TheOne Nov 17 '11 at 18:47
  • 2
    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. – Andrew Feb 16 '16 at 17:39
  • Thank you sooo much @NT3RP! – Pyzard Sep 23 '20 at 22:25
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

  • This is surely the easiest option, but I think it's far slower than the others. – Nicholas Obert Jan 31 '22 at 08:08
  • 1
    @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. – miksus Apr 11 '22 at 05:29
  • Yes, it probably isn't going to make much difference, but it's still worth pointing out to whoever may opt for this solution. – Nicholas Obert Apr 14 '22 at 05:30