turned on gray laptop computer
Photo by Oskar Yildiz on Unsplash

You have probably come across situations in which you needed to convert a string into a date in Python. There are a few ways to do that, but you may have not had the chance to use the following method.

It’s not that it is so special that you cannot learn, or that it is so difficult what’s going on. It’s basically a rare conversion that you have not had the chance to stumble upon before.

Despite being engineers, we still spend a considerable amount of time using a natural language to talk. 

Since that is the case, we can also convert a string looking like a natural phrase into a date as shown below:

In this example:

  • %d — represents the day of the month, like 21, 22, 23, etc.
  • %B — represents the actual name of the month like January, February, March etc.
  • %Y — represents the year, like 2020, 2021, etc.

That’s pretty much it.

I hope you found it useful.