Js Date object to python datetime

后端 未结 3 1085
情深已故
情深已故 2021-01-01 23:07

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

相关标签:
3条回答
  • 2021-01-01 23:16

    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")
    
    0 讨论(0)
  • 2021-01-01 23:17

    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)
    
    0 讨论(0)
  • 2021-01-01 23: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

    0 讨论(0)
提交回复
热议问题