Home > Python > How to Print Date and Time in Python?

How to Print Date and Time in Python?

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.

To import datetime
Syntax

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.

Output
2022-06-01 18:01:29.481113

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.

Output
2022-06-01

You can also get the current date in another way by importing only a single date object from the whole datetime module.

Output
2022-06-01

Print Current Timestamp

The timestamp has encoded information of date and time in the sequences of characters. So we will use the time and datetime module to get the current timestamp in python program.

Using datetime module:

Output
1654108309.620273

In the above code, we used the now() method of datetime class and then used the timestamp() method to get the current timestamp.

Using time module:

Output
1654108309.620273

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 datetime and time module.

Now we will see how we can get the date from the timestamp using the fromtimestamp() method of the date object.

Output
2022-06-01

Uses of now() Attributes

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.

Output
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 datetime and time module.

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.

Output
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 datetime() method.

Output
2022-06-02 12:45:22.346864

Conclusion

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 time module.

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.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment