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'

Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69
  • You might also be interested in this other [issue](https://stackoverflow.com/questions/29284850/datetime-strptime-set-format-to-ignore-trailing-part-of-string) – Nakor Jul 12 '19 at 20:16

2 Answers2

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.

Sebastin Santy
  • 992
  • 1
  • 7
  • 13
-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

  • Very interesting, What is that syntax you are using for the function? – Kristoffer Tølbøll Jul 12 '19 at 20:22
  • [Python type annotation](https://dev.to/dstarner/using-pythons-type-annotations-4cfe), 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. – henrikhorluck Jul 12 '19 at 20:27
  • Very nice trick actually, I was not aware you could do this. I have only tried something similar using the dataclass API from python. – Kristoffer Tølbøll Jul 12 '19 at 20:29
  • 1
    It can be quite helpful indeed, especially if paired with [docstrings](https://www.datacamp.com/community/tutorials/docstrings-python), 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 – henrikhorluck Jul 12 '19 at 20:41
  • @baileyhaldwin did you remember to change the format of the string sent from your frontend? – henrikhorluck Jul 16 '19 at 14:21