The current datetime is passed via an ajax request to a django backend where it will be stored in the database. To store it in the database, the date must first be converted to a datetime
object which can be done for a date of the in UTC format (Eg. Sun, 04 Sep 2016 07:13:06 GMT
) easily by the following statement:
>>> from datetime import datetime
>>> datetime.strptime("Sun, 04 Sep 2016 07:13:06 GMT", "%a, %d %b %Y %H:%M:%S %Z")
However in such a method, there is no preservation of the user's timezone.
The javascript Date
constructor call i.e. new Date()
returns a date in the following format:
Sun Sep 04 2016 12:38:43 GMT+0530 (IST)
which gives an error when converting to datetime object.
>>> datetime.strptime("Sun, 04 Sep 2016 07:13:06 GMT+0530 (IST)", "%a, %d %b %Y %H:%M:%S %Z")
ValueError: time data 'Sun Sep 04 2016 12:46:07 GMT+0530 (IST)' does not match format '%a, %d %b %Y %H:%M:%S %Z'
1) How to resolve this problem? 2) Is there any better way to approach it?