Python is one of the most popular programming languages in today’s world and it is used in different fields including web development, data analytics, and in mathematical computations. And one of the common tasks we do in python is to get the current date and time in python.
Here, in this short guide, we will see different ways to get the current date and time in python.
Get the current date and time in python
In python, we can use its in-built python libraries like
datetime module or the
time module to work with date and time.
Since it’s in-built, we don’t have to install it in our project, we just need to import it, and it’s good to go.
Using the datetime Module
datetime module is a very common module that provides classes to manipulate dates and times in python. It also provides functions for formatting and parsing dates and times.
Here, is a simple example to get the current date and time in python.
from datetime import datetime current_date_time = datetime.now() print("Current date and time:", current_datetime)
This code will output the current date and time in this format:
Current date and time: 2023-02-08 12:50:30.440684
Get only the current date using datetime module
Now, if you only want the current date and not the time, then we can use the
date class provided by the module.
from datetime import date current_date = date.today() formatted_current_date = current_date.strftime("%d-%m-%Y") print("Current date:", formatted_current_date)
This will only give us the current date as the output:
Current date: 08-02-2023
Here, we have imported the
date from datetime module and got the current date, and then used
strftime("%d-%m-%Y") function to format the date in
Get only current time using datetime module
If you want to get only current time separately, then we have to use the
strftime() function in the code. The
strftime() function also helps us to format our time in
from datetime import datetime current_date_time = datetime.now() current_time = current_date_time.strftime("%H:%M:%S") print("Current time:", current_time)
The above code will give us a formatted current time:
Current time: 12:58:14
Using the time Module
We can also use the
time module to get the current date and time in python. It is used to handle time values and also provide functions for formatting them into strings.
Get only date using time Module
So, to get the current date we can use the following code:
import time current_date = time.strftime("%d-%m-%Y") print(current_date)
This code will output the current date in dd/mm/yyyy as:
Current date: 08-02-2023
Get only the time using time Module
To get only the current time we can use the following code:
import time current_time = time.strftime("%H:%M:%S") print("Current time:", current_time)
This will give us the current time as output in hh/mm/ss format:
Current time: 13:31:49
In this article, we have seen different ways to get the current date and time using the
time modules. These two modules provide everything that is needed to work with time and date in python. You can use any of the methods in your program depending on your needs.