python f-string format date 1

python f-string format date

import datetime
now = datetime.datetime.now()
print(f'{now:%Y-%m-%d %H:%M}')

Here is what the above code is Doing:
1. We import the datetime module.
2. We create a variable called now and set it equal to datetime.datetime.now().
3. We print the value of now, but we use a format string to specify how we want the date and time to be displayed.

The format string we use is %Y-%m-%d %H:%M. This format string displays the year, month, day, hour, and minute.

The %Y, %m, and %d are all format codes for replacing those values in the string. The %H is a format code for the hour, and %M is a format code for the minute.

The format codes are replaced with the values from the now variable.

The output of the above code will look something like this:

2019-11-21 15:07

Similar Posts