In Python, how do you convert a string in the format "Jan 01, 2001" into a date?
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 :
-
%bis the month as locale’s abbreviated name; -
%dis the day of the month as a zero-padded decimal number; -
%Yis the year with century as a decimal number.
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 :
-
%bis the month as locale’s abbreviated name; -
%dis the day of the month as a zero-padded decimal number; -
%Yis the year with century as a decimal number.
| # | ID | Query | URL | Count |
|---|---|---|---|---|
| 0 | 12949 | en | https://en.ans.wiki/5811/in-python-how-do-you-convert-a-string-in-the-format-jan-01-2001-into-a-date | 6 |