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 :
-
%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.
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.
# | ID | Query | URL | Count |
---|