Posted on 21st July 2024|12 views
How to convert datetime objects to a string?
Posted on 21st July 2024| views
The following program converts a datetime object holding the current date and time to various string forms.
from datetime import datetime
now = datetime.now() # (now)current date and time
year = now.strftime("%d")
print("day:", day)
month = now.strftime("%m")
print("month:", month)
day = now.strftime("%Y")
print("year:", year)
Output:
day: 24
month: 6
year: 2020