Current date and time
In this article, you will learn to get today's date and current date and time in Python. We will also format the date and time in different formats using strftime() method.
There are a number of ways you can take to get the current date. We will use the date
class of the datetime module to accomplish this task.
Example 1: Python get today's date
Here, we imported the date
class from the datetime
module. Then, we used the date.today()
method to get the current local date.
By the way, date.today()
returns a date
object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.
Example 2: Current date in different formats
When you run the program, the output will be something like:
If you need to get the current date and time, you can use datetime
class of the datetime
module.
Example 3: Get the current date and time
Here, we have used datetime.now()
to get the current date and time. Then, we used strftime()
to create a string representing date and time in another format.
Last updated