In this example tutorial, we will learn about how to print the date and current date time in the python program. We will also see some examples of printing dates and times in different formats.
Python has a built-in module
datetime to get the dates and times, so we will use the datetime module in the python program to print the date and time and also the current date and time.
Let’s play with some examples of using the datetime module and printing the datetime in different formats.
Print Date and Time in Python
To print the date and time in the python program, you need to import the
datetime module in your file and then use their functions to get the date and time from the module object.
It will return the current date and time in time format. The tz refer to time zone, default is Greenwich Meridian.
How to use datetime?
Print Current Date and Time
To print the current date and time in python, you have to first import the
datetime module and then use the
now() method. See the following example code.
Print Current Date Only
If you want to get the only date from datetime class then you can use the
today() method with the
date object to print the date. See the following examples.
You can also get the current date in another way by importing only a single
date object from the whole
Print Current Timestamp
The timestamp has encoded information of date and time in the sequences of characters. So we will use the
datetime module to get the current timestamp in python program.
In the above code, we used the
now() method of datetime class and then used the
timestamp() method to get the current timestamp.
In the above code, we used the time module and then its time() method to get the current timestamp in python.
Get Date From Timestamp
In the above two examples, we see how we can get the current timestamp using the
Now we will see how we can get the date from the timestamp using the
fromtimestamp() method of the
As you see in the above examples, we used the
now() method of datetime object to print the current date and time.
Do you know that you can also print the year, month, day, hour, minute, second, and microsecond? No worry!!! I will show you.
To get the year, month, day, hour, minute, second, and microsecond from the
now() method, See the following example code.
Year : 2022 Month : 6 Day : 1 Hour : 18 Minute : 55 Second : 9 Microsecond : 460453
Create and Print Time
You can also print the time in the python program by importing the time class from
You can create the time by specifying the hours, minutes, and seconds as parameters in the
time() method. Let’s see some examples of creating time in different ways and print them.
00:00:00 12:22:45 12:56:33 10:24:44.567658
Print Full Date and Time
To create and print the full datetime in python, we will import the
datetime object from
datetime module. Then specify the everything in
So in this tutorial, you learned about how to get a date and time in python and print it. You see the different ways to get the datetime using the
datetime module and
Hope you like it and share it with your friends. For any queries please ask in the comment section, I will respond to you as soon as possible.