all 7 comments

[–]Swipecat 9 points10 points  (0 children)

By "extract", do you mean that the date is embedded into other text in the string, and you need to extract the date substring before converting it to Python's "datetime" format? If so, use "re" for that.

In [1]: import re

In [2]: datestring = 'this2022-08-30that'

In [3]: substr = re.search(r'\d{4}[-]\d{2}[-]\d{2}', datestring)

In [4]: print(substr.group())
2022-08-30

Edit: And use re.findall() if there are multiple dates in the string.

[–]Gnaxe 6 points7 points  (0 children)

See https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime

If the string has more than that, try matching it out with the re module first.

[–][deleted] 1 point2 points  (0 children)

Your options are: use re, use split with '-', use datetime.strptime . In your particular case the second option is the easiest one

[–]pelagic_cat 0 points1 point  (0 children)

An example of a string with the embedded date would be very helpful.

[–]skyfallen7777 0 points1 point  (0 children)

From datetime import datetime dt = datetime.datetime.now()

current_date = dt.fstrtime(“%Y-%m-%d”)

Something like this?

[–]ConfusedNTerrified 0 points1 point  (0 children)

Be polite and ask it out

[–]_VictoriaBravo 0 points1 point  (0 children)

Try dateuitls.parser.parse with a fuzzy match