Question #5811   Submitted by Answiki on 01/25/2022 at 08:33:57 PM UTC

In Python, how do you convert a string in the format "Jan 01, 2001" into a date?

Answer   Submitted by Answiki on 01/25/2022 at 08:36:52 PM UTC

In Python, the best way to convert a string into a date is to use the dedicated function strptime() from the datetime module. Here is an example:

>>> from datetime import datetime
>>> datetime.strptime('Jan 02, 2021', '%b %d, %Y')
datetime.datetime(2021, 1, 2, 0, 0)

The first parameter of the strptime() function is the string to convert. The second parameters is the format of tha string. In the above example :

  • %b is the month as locale’s abbreviated name;
  • %d is the day of the month as a zero-padded decimal number;
  • %Y is the year with century as a decimal number.
2 events in history
Answer by Answiki on 01/25/2022 at 08:36:52 PM

In Python, the best way to convert a string into a date is to use the dedicated function strptime() from the datetime module. Here is an example:

>>> from datetime import datetime
>>> datetime.strptime('Jan 02, 2021', '%b %d, %Y')
datetime.datetime(2021, 1, 2, 0, 0)

The first parameter of the strptime() function is the string to convert. The second parameters is the format of tha string. In the above example :

  • %b is the month as locale’s abbreviated name;
  • %d is the day of the month as a zero-padded decimal number;
  • %Y is the year with century as a decimal number.
Question by Answiki 01/25/2022 at 08:33:57 PM
In Python, how do you convert a string in the format "Jan 01, 2001" into a date?
# ID Query URL Count

Icons proudly provided by Friconix.