How to print a date in a regular format?
In Python, you can use the datetime module to work with dates and times. To print a date in a regular format, you can use the strftime()
method of the datetime object. This method allows you to specify a format string that defines the desired format of the date.
Here's an example of how to use strftime()
to print the current date in the format "YYYY-MM-DD":
from datetime import datetime
# Get the current date
now = datetime.now()
# Use strftime() to format the date
date_string = now.strftime("%Y-%m-%d")
print(date_string)
# Output: "2022-12-01"
In this example, the format string "%Y-%m-%d"
is used to specify that the year should be printed as four digits, followed by a dash, the month as two digits, another dash and the day as two digits.
You can also use the following format codes to print the date in different format %d : Day of the month as a zero-padded decimal number. %m : Month as a zero-padded decimal number. %y : Year without century as a zero-padded decimal number. %Y : Year with century as a decimal number. %a : Weekday as locale short version %A : Weekday as a full version %b : Month name as a short version %B : Month name as a full version
You can refer to python documentation for more information on strftime and different format codes.