(Python Tutorial – 046)
How to get current date and time in Python?
In this article, you will learn to get today’s date and current date and time in Python. We will also format the date and time in different formats using strftime() method.
There are a number of ways you can take to get the current date. We will use the
date class of the datetime module to accomplish this task.
Example 1: Python get today’s date
from datetime import date today = date.today() print("Today's date:", today)
Here, we imported the
date class from the
datetime module. Then, we used the
date.today() method to get the current local date.
By the way,
date.today() returns a
date object, which is assigned to the today variable in the above program. Now, you can use the strftime() method to create a string representing date in different formats.
Example 2: Current date in different formats
from datetime import date today = date.today() # dd/mm/YY d1 = today.strftime("%d/%m/%Y") print("d1 =", d1) # Textual month, day and year d2 = today.strftime("%B %d, %Y") print("d2 =", d2) # mm/dd/y d3 = today.strftime("%m/%d/%y") print("d3 =", d3) # Month abbreviation, day and year d4 = today.strftime("%b-%d-%Y") print("d4 =", d4)
When you run the program, the output will be something like:
d1 = 03/07/2020 d2 = July 03, 2020 d3 = 07/03/20 d4 = Jul-03-2020
If you need to get the current date and time, you can use
datetime class of the
Example 3: Get the current date and time
from datetime import datetime # datetime object containing current date and time now = datetime.now() print("now =", now) # dd/mm/YY H:M:S dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print("date and time =", dt_string)
Here, we have used
datetime.now() to get the current date and time. Then, we used
strftime() to create a string representing date and time in another format.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.