Get Current Date & Time in Python (4 Examples)

 

This page illustrates how to return the actual date and time in the Python programming language.

Table of contents:

Let’s get started: We first have to load the datetime module:

from datetime import date                          # Import datetime module

 

Example 1: Show the Current Local Date

In Example 1, I’ll show how to get the present date by using the date.today function.

Consider the codebox below:

my_date_today = date.today()                       # Get current date
print(my_date_today)                               # Show current date
# 2022-06-09

 

Example 2: Current Date in Another Format (mm/dd/y)

If we want to get the date plus adapt it to another structure, we can use the date.today function combined with the strftime function and the wanted structure:

my_date_1 = my_date_today.strftime("%m/%d/%y")     # Using strftime function
print(my_date_1)
# 06/09/22

In this case, we get the output 06/09/22 (month/day/year), since we used strftime(“%m/%d/%y”).

Example 3: Current Date in Different Format (Textual month, day and year)

Of course, it’s also possible to set any other date and time formats. Have a look at the below Python syntax:

my_date_2 = my_date_today.strftime("%B %d, %Y")    # Using strftime function
print(my_date_2)                                   # Print current date with changed format
# June 09, 2022

In this example, we let the month return as full name. Here you can find a summary of date and time format codes.

 

Example 4: Get Actual Date and Time

In case we want to extract not only the present date but also the present time, we can do the following steps.

First, we need to load datetime from the datetime module:

from datetime import datetime                      # Load datetime from datetime module

Next, we can apply the datetime.now function which returns us a datetime object including the current date and time:

my_datetime = datetime.now()                       # Applying datetime.now function
print(my_datetime)                                 # Show current date and time
# 2022-06-09 11:15:36.820458

Looks good!

 

Video & Further Resources

Would you like to know more about the printing of the actual time and date? Then I recommend having a look at the following video on my YouTube channel. In the video, I demonstrate the topics of this article in Python:

 

 

In addition, you could read the other articles on https://statisticsglobe.com/:

 

In this Python tutorial, you have learned how to show the current date and time. If you have any additional questions, please let me know in the comments.

 

Matthias Bäuerlen Python Programmer

This page was created in collaboration with Matthias Bäuerlen. Have a look at Matthias’ author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top