-1

I'm scraping some data and I got this format of date: February 23rd and would like to change it to 2021-02-23 using Python.

Thanks in advance

AlexBrunet
  • 85
  • 8
  • This is how you use [strftime](https://stackoverflow.com/questions/50256825/datetime-strftime-and-strptime) you can learn more about what each tags mean by `man strftime` if on unix based system – mTvare Feb 25 '21 at 17:18
  • @mTvare `strftime` would work in most cases, but it doesn't play very nice when dealing with strings that have `th`, `st`, or `rd`. Also, since the string is missing the year value, `strftime` would default to `1900` – Wondercricket Feb 25 '21 at 17:24

1 Answers1

1

I recommend using the dateutil module for parsing strings into dates that have non-standard formats that otherwise strftime could handle

from dateutil.parser import parse    

print(parse('February 23rd'))

>>> 2021-02-23 00:00:00

Since the year is missing from the input, parse defaults to current year

Wondercricket
  • 7,651
  • 2
  • 39
  • 58