Python

Get current date in python

The current date must be read for different types of programming purposes. Python has different modules to get the current date. The datetime module is one of them. It is a built-in module of Python. So, you don’t need to install it. To read the current date, you have to import this module into your script. The uses of this module to get the current date have shown in this tutorial.

Example-1: Get current date using today() function

The today() function is one of the useful functions of the datetime module to read the current date. The syntax of today() function is given below.

Syntax:

date date.today()

The today() function has no argument. If the date class is imported from the datetime module, then the today() function can be used to get the current date value. The use of this function has shown in the following example. Create a python file with the following script to read the current local date in a different format by using today(), and strftime() functions. The default value returned by the today() function will be printed in the first output. The formatted value of the current date will be printed in the second and the third outputs.

# Import date from datetime module
from datetime import date

# Read the current date from the system
current_date = date.today()
# Print the current date without formatting
print("Today is: ", current_date)

# Print the formatted date with short month name
formatted_date1 = current_date.strftime("%d-%b-%Y")
print("Today is: ", formatted_date1)

# Print the formatted date with full month name
formatted_date2 = current_date.strftime("%B %d, %Y")
print("Today is: ", formatted_date2)

Output:

The following output will appear after executing the above script.

Example-2: Get current date using now() function

The now() function is another useful function of the datetime module to read the current date and time. The syntax of the now() function is given below.

Syntax:

datetime datetime.now(timezone)

The datetime module is required to import before using this function. It has an optional argument that is used to specify the time zone, and the current date and time will be retrieved based on this value. It returns the current date and time in time format. The use of this function has shown in the following example. Create a Python file with the following script to print the current date and time. The datetime module has been imported at the beginning of the script to use the now() function. Next, the returned value and the formatted output of the returned value have been printed. Here, the strftime() function has been used to print the current date only from the output of the now() function.

# Import datetime from datetime module
from datetime import datetime

# Read the current date from the system
Current_datetime = datetime.now()
# Print the current date and time
print("The current date and time is: ", Current_datetime)
# Print the current date
print("Today is: ", Current_datetime.strftime("%d-%m-%Y"))

Output:

The following output will appear after executing the above script.

Example-3: Read different parts of the current date separately

The now() function has many attributes to retrieve the current date and time parts, such as day, month, year, hour, minute, etc. Create a python file with the following script to read the day, month, and year values of the current dates separately and marge the values to print the current formatted date.

# Import datetime from datetime module
from datetime import datetime

# Read the current date and time
current_datetime = datetime.now()

print("Print the current date using different attributes:")

# Read the current day of the date
cur_day = current_datetime.day
# Read the full month of the current date
cur_month = current_datetime.strftime("%B")
# Read the full year of the current date
cur_year = current_datetime.year
# Print the current date by merging the day, month, and year values
print("Today is %s %d, %d" % (cur_month, cur_day, cur_year))

Output:

The following output will appear after executing the above script.

Conclusion:

The uses of two different functions of the datetime module have been shown in this tutorial to get the current date by using python script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.