You can use the datetime
module in Python to break up a date into day, month, and year. First, you need to create a datetime object using the date string. Then, you can get the individual day, month, and year components using the day
, month
, and year
attributes of the datetime object, respectively. Here is an example:
main.py331 chars17 lines
Output:
main.py29 chars4 lines
In the above example, we first define the date string '2022-11-30'
. Then, we use the strptime
method of the datetime
class to parse the date string into a datetime
object dt
, using the format string '%Y-%m-%d'
which specifies the year (%Y
), month (%m
), and day (%d
) components of the string. Finally, we extract the day, month, and year components of dt
using the day
, month
, and year
attributes, respectively, and print them to the console.
gistlibby LogSnag