datetime

To print the current date, we can utilize the datetime module. Dates are objects, like anything in Python. Datetime has several subclasses, which can be confusing at first.

Related course:

Date Example

We can use its method strftime() to change the format.
To show the current date (today):

This will show the date in the format

YYYY-MM-DD

If you want another format, use the method strftime():

This will output

DD-MM-YYYY

Get current time in Python

You can get the current time like this:

To get the date and time, use:

Format codes

When using the method strftime() we can specify the type of date format we want to show.
So which format codes do we have?

Previous Post

Leave a Reply

Your email address will not be published. Required fields are marked *