How to print today's date in different formats - convert datetime to string
0
8440
In this session we will print datetime in different formats.The purpose is to convert python datetime object to string
The datetime module supplies classes for manipulating dates and times in both simple and complex ways. strftime() converts a datetime object to string. The strftime() takes formats with which you want the string to be converted. The formats are here https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
Default format:
from datetime import datetime
today_date = datetime.today()
print(today_date)
YY-MM-DD format
from datetime import datetime, date
print("Current date With YY-MM-DD format is : ", datetime.now().strftime("%y-%m-%d"))
print("Current date With YY-MM-DD format is : ", date.today().strftime("%y-%m-%d"))
YYYY-MM-DD format
from datetime import datetime, date
print("Current date with YYYY-MM-DD format is : ", datetime.now().strftime("%Y-%m-%d"))
print("Current date with YYYY-MM-DD format is : ", date.today().strftime("%Y-%m-%d"))
Comment here