Introduction
Whether or not you are logging occasions or measuring execution instances, you will typically end up working with dates. In Python, the built-in datetime
module makes it straightforward to get the present date and time, format it, and even do time math. On this Byte, we’ll concentrate on the best way to get right now’s date and format it within the YYYY-MM-DD
format.
Dates and Instances in Python
Python offers the datetime
module in its normal library for coping with dates and instances. This module consists of a number of courses for managing dates, instances, timedeltas, and extra. The 2 courses we’ll be specializing in on this Byte are datetime
and date
.
The datetime
class is a mixture of a date and a time, and offers a variety of strategies and attributes. The date
class, however, is solely involved with dates (yr, month, and day).
Here is a fast instance of how one can create a datetime
object:
from datetime import datetime
# create a datetime object
dt = datetime(2023, 9, 19, 23, 59, 59)
print(dt) # Output: 2023-09-19 23:59:59
On this snippet, we’re making a datetime
object for the final second of Sept. nineteenth, 2023. However how can we get the present date?
Getting At present’s Date in Python
Python’s datetime
module offers a technique referred to as right now()
that returns the present date and time as a datetime
object. Here is how you need to use it:
from datetime import datetime
# get right now's date
right now = datetime.right now()
print(right now) # Output: 2023-09-19 22:17:08.845750
Within the above instance, the right now()
technique returned the present date and time. Nonetheless, in case you solely want the date, you need to use the date()
technique of a datetime
object to get a date
object:
from datetime import datetime
# get right now's date
right now = datetime.right now().date()
print(right now) # Output: 2023-09-19
Formatting Date as YYYY-MM-DD
The date
and datetime
objects present a technique referred to as strftime()
that means that you can format the date and time in numerous methods. The strftime()
technique takes a format string the place every %
-prefixed character is changed with information from the date and time.
To format a date within the YYYY-MM-DD
format, you need to use the %Y
, %m
, and %d
format codes:
from datetime import datetime
# get right now's date
right now = datetime.right now().date()
# format date as YYYY-MM-DD
formatted_date = right now.strftime('%Y-%m-%d')
print(formatted_date) # Output: 2023-09-19
Within the format string, %Y
is changed with the four-digit yr, %m
is changed with the two-digit month, and %d
is changed with the two-digit day.
Notice: The strftime()
technique returns a string, so you may’t use date strategies or attributes on the outcome. If you’ll want to manipulate the date after formatting it, maintain a reference to the unique date
or datetime
object.
And that is it! You now know the best way to get right now’s date in Python and format it within the YYYY-MM-DD
format.
Different Methods to Get At present’s Date
Whereas the datetime
module is a strong software for working with dates and instances in Python, it is not the one method to get right now’s date. Let’s discover another strategies.
One various is utilizing the time
module, one other built-in Python module for coping with time. Here is how you need to use it to get right now’s date:
import time
right now = time.strftime("%Y-%m-%d")
print(right now) # Output: 2023-09-19
Once you run this code, you will get the present date output within the ‘YYYY-MM-DD’ format. The strftime
technique codecs the time in line with the given format string.
Notice: Whereas the time
module can provide the present date, it doesn’t have as many date and time manipulation capabilities because the datetime
module. It is usually higher to make use of datetime
for extra complicated date and time duties.
An alternative choice is to make use of an exterior library, like pendulum
. Pendulum is a Python library that simplifies and enhances date dealing with, even past what’s obtainable in datetime
.
Here is how one can get right now’s date with pendulum
:
import pendulum
right now = pendulum.now().to_date_string()
print(right now) # Output: 2023-09-19
This may even provide the present date within the ‘YYYY-MM-DD’ format. The now
technique will get the present date and time, and the to_date_string
technique codecs it as a string.
Notice: Do not forget that pendulum
just isn’t a built-in module, so you will want to put in it utilizing pip (pip set up pendulum
) earlier than you need to use it.
Conclusion
We have lined a wide range of methods to get right now’s date in Python and format it as ‘YYYY-MM-DD’. We began off with the fundamentals of dates and instances in Python, then moved on to getting right now’s date utilizing the datetime
module. We additionally checked out the best way to format the date within the ‘YYYY-MM-DD’ format. Lastly, we explored another strategies for getting the present date, utilizing each the built-in time
module and the exterior pendulum
library.