How to get today date in Python without time?
Short guide to get the only today's date without the time in python using the datetime and time modules. And also learn to format the date in dd/mm/yyyy format using strftime function.
Python is the most popular and versatile programming language in today's world. It is used for web development, data analysis and to perform scientific computation at ease. One of the most common tasks while working with python is to get the current date and time. However, in this short article, we will only try to get the date without the time in python.
Get the current date-time in Python
Usually, when we want today's date we use the datetime in-built python library like this:
import datetime current_date = datetime.datetime.today() print(current_date)
Output:
2023-02-07 15:12:25.475641
As you can see, the datetime library gives us the non-formatted current date along with the time.
So how can we get only today's date and not the time in python?
Well, it's very easy to get using the same datetime and the time library in python.
Get only today's date without the time in python
So to get only the current date without the time we can write the following code:
from datetime import date today_date = date.today() print(today_date)
Output:
2023-02-07
As you can see, the output of the above code is only today's date i.e 2023-02-07 without the time. Here, we have imported only the date function from the datetime library.
The default format will be yyyy/mm/dd. However, we can easily change it to the dd/mm/yyyy format using the strftime() method.
The
strftime()function returns string representation of date and time object in python.
So to format the time, we can use the strftime() function like this in the code:
from datetime import date today_date = date.today().strftime("%d-%m-%Y") print(today_date) # 07-02-2023
So this will give us the output in dd/mm/yyyy date format i.e 07-02-2023
Get today's date using in-built time module
We can use another in-built module in python to get today's date without the time. This library is the python time library.
So using the time module, we can get the current date like this:
import time today_date = time.strftime("%d-%m-%Y") print(today_date)
Output:
07-02-2023
Here, we have imported time module and used the strftime() function to get the current date in a properly formatted order.
Conclusion:
In python, getting today's date is very easy by using the in-built datetime or the time library. By following the process shown above, you can easily get the current date without the time component.
Related Articles:
Related Posts
How to merge JSON files in python
Learn different techniques to merge multiple JSON files in Python using built-in JSON module and Pandas library. Includes examples for combining data arrays and merging dictionaries by key.
Pytube Description and Keyword Not Showing: Solution
Solve the issue with Pytube not showing youtube description and tags in this article.
How to Fix the subprocess-exited-with-error in Python
Learn what cause the subprocess-exited-with-error in Python and also learn how to solve the error in your code.
Python Integer Division: The Floor Division Operator Explained
Article on Python Integer Division operator (//) with examples and the difference between standard division and floor division.
