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:
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.
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:
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.
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.
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.