0

I have a javascript date object, which i turn into a string, and send to my backend to parse using python.

here is an example of the javascript date string 'Mon Aug 18 2014 21:11:54 GMT+0200 (Centraleuropæisk sommertid)

I have made a python helper function to help parse the date.

def datetime_converter(datetime_str):
    return datetime.strptime(datetime_str, "%a, %d %b %Y %H:%M:%S %Z")

this should correctly parse the date according to this answer

i think the problem is that there is an extra text attached to the end (It's danish since I'm from Denmark) (Centraleuropæisk sommertid)

does anybody know a workaround for this? i could split the string for a ( but that does not seem like a good solution.

Here is the exception i get data 'Mon Aug 18 2014 21:11:54 GMT+0200 (Centraleuropæisk sommertid)' does not match format '%a, %d %b %Y %H:%M:%S %Z'

1
  • You might also be interested in this other issue Commented Jul 12, 2019 at 20:16

2 Answers 2

2

This should work for your case:

from email import utils
utils.parsedate_to_datetime('Mon Aug 18 2014 21:11:54 GMT+0200 (Centraleuropæisk sommertid)')

You can convert in the format you want hereon.

Sign up to request clarification or add additional context in comments.

4 Comments

What does this return?
returns datetime.datetime(2014, 8, 18, 21, 11, 54)
Though this doesn't honor the timezone. If you remove GMT from the string, it will.
@SebastinSanty the problem is that removing the GMT part would be quite a hassle, because the language/format can be different for each language. So splitting it would cause some problems.
-1

So I would suggest using the javascript Date .toISOString() method (MDN Docs) instead, which gives you an ISO8601-formatted date-string (YYYY-MM-DDTHH:MM:SSZ), and then you could simply change the parser function accordingly:

def datetime_converter(datetime_str: str) -> datetime:
    return datetime.strptime(datetime_str, "%Y-%m-%dT%H:%M:%S%z")

Since python 3.6, you are also able type-annotate your python code, following the PEP-528 standard

5 Comments

Very interesting, What is that syntax you are using for the function?
Python type annotation, which, if you are working on larger projects, or just want to read code you wrote 3 months ago, can be quite helpful in finding out how a function is supposed to be used.
Very nice trick actually, I was not aware you could do this. I have only tried something similar using the dataclass API from python.
It can be quite helpful indeed, especially if paired with docstrings, and more sensible function name, like parse_ISO8601, and a docstring like: '''Parses an ISO8601 date into a python datetime object Arguments: date_string (str): An ISO8601 formatted datetime string Returns: datetime:A datetime-object representing the time of the input-string ''' However in this case that is unnecessary
@baileyhaldwin did you remember to change the format of the string sent from your frontend?

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.