You can use the strftime
method from the datetime
module to format a date into the desired string format. Here's an example code:
main.py314 chars8 lines
Explanation:
datetime
module.date_str
as "2014-10-13".strptime
method from the datetime
module to convert the input date string into a datetime
object date_obj
.%Y-%m-%d
format string represents the order of year-month-day in the input date string. For example, 2020-01-11
has year 2020
, month 01
and day 11
.strftime
method from the datetime
module to format the date_obj
into a string format of %B %d, %Y
.%B
directive outputs the full name of the month (e.g. "January", "February", etc).%d
directive outputs the day of the month as a zero-padded decimal number (e.g. "01", "02", ..., "31").%Y
directive outputs the year with century as a decimal number.formatted_date
.formatted_date
variable which outputs "October 13, 2014" as the result.gistlibby LogSnag