Get current date and time in Python
In this article, we will learn how to get the current date and time in Python. We will use the built-in module available for getting date and time information and some custom codes as well to see them working. Let's first have a quick look over what are dates in Python.
Date in Python
In Python, we can work on Date functions by importing a built-in module datetime
available in Python. We have date objects to work with dates. This datetime module contains dates in the form of year, month, day, hour, minute, second, and microsecond. The datetime module has many methods to return information about the date object. It requires a date, month, and year values to compute the function. Date and time functions are compared like mathematical expressions between various numbers.
There are various ways in which we can get the current date and time using date and time objects. We will import date
and time
class from datetime
module and will print the output in different formats. Look at the examples below to learn different ways to get current date and time.
Example: Get Current Date Using today() Function
The below example uses today()
function to compute the current date. It is a part of the datetime
module. It returns date in yyy-mm-dd
format.
from datetime import date
current_date = date.today()
print("Current Date:", current_date)
Current Date: 2021-01-18
Example: Get Current date and time with different formats
The below example uses today()
function to store the current date in a variable. It is a part of the datetime module. The program uses strftime()
function to print different date formats by passing formats as an argument. It returns date in four different formats as shown below.
from datetime import date
current_date = date.today()
date1 = current_date.strftime("%d/%m/%Y")
print("Date in date/month/year format =", date1)
date2 = current_date.strftime("%B %d, %Y")
print("Date in month/day/year format =", date2)
date3 = current_date.strftime("%m/%d/%y")
print("Date in month/date/year format =", date3)
date4 = current_date.strftime("%b-%d-%Y")
print("Date in month/date/year format =", date4)
Date in Date/Month/Year format = 18/01/2021
Date in Month Name Date, Year format = January 18, 2021
Date in Month/Date/Year format = 01/18/21
Date in Month-Date-Year format = Jan-18-2021
Example: Get Current date and time using now() Function
The below example uses now()
function to compute the current date and time. It is a part of the datetime module. It returns date as well as time in yyy-mm-dd hh:mm:ss: ms
format. It also uses strftime()
function to print date and time in a different format.
from datetime import datetime
current_date = datetime.now()
print("Current Date and Time=", current_date)
string = current_date.strftime("%d/%m/%Y %H:%M:%S")
print("Current Date and Time in different format=", string)
Current Date and Time= 2021-01-18 08:45:24.633526
Current Date and Time in different format= 18/01/2021 08:45:24
Example: Get Current time using now() Function
The below example uses now()
function to compute the current time. It imports datetime from datetime module. Using datetime
object, now()
and time()
, value is stored in a variable. It returns time in hh-mm-ss-ms
format.
from datetime import datetime
current_time = datetime.now().time()
print("Current Time:", current_time)
Current Date: 08:56:39.372822
Conclusion
In this article, we learned to get the current date and time either by creating datetime object to get both date and time or by creating a date object to get date only. This is achieved using today() and now() functions. We learned to print date and time in different formats. For this, we used strftime() function. We used some custom codes as well to better understand the functions.