Getting Today's Date in YYYY-MM-DD in Python
Introduction
Whether you're logging events or measuring execution times, you'll often find yourself working with dates. In Python, the built-in datetime
module makes it easy to get the current date and time, format it, or even do time math. In this Byte, we'll focus on how to get today's date and format it in the YYYY-MM-DD
format.
Dates and Times in Python
Python provides the datetime
module in its standard library for dealing with dates and times. This module includes several classes for managing dates, times, timedeltas, and more. The two classes we'll be focusing on in this Byte are datetime
and date
.
The datetime
class is a combination of a date and a time, and provides a wide range of methods and attributes. The date
class, on the other hand, is solely concerned with dates (year, month, and day).
Here's a quick example of how you 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
In this snippet, we're creating a datetime
object for the last second of Sept. 19th, 2023. But how do we get the current date?
Getting Today's Date in Python
Python's datetime
module provides a method called today()
that returns the current date and time as a datetime
object. Here's how you can use it:
from datetime import datetime
# get today's date
today = datetime.today()
print(today) # Output: 2023-09-19 22:17:08.845750
In the above example, the today()
method returned the current date and time. However, if you only need the date, you can use the date()
method of a datetime
object to get a date
object:
from datetime import datetime
# get today's date
today = datetime.today().date()
print(today) # Output: 2023-09-19
Formatting Date as YYYY-MM-DD
The date
and datetime
objects provide a method called strftime()
that allows you to format the date and time in various ways. The strftime()
method takes a format string where each %
-prefixed character is replaced with data from the date and time.
To format a date in the YYYY-MM-DD
format, you can use the %Y
, %m
, and %d
format codes:
from datetime import datetime
# get today's date
today = datetime.today().date()
# format date as YYYY-MM-DD
formatted_date = today.strftime('%Y-%m-%d')
print(formatted_date) # Output: 2023-09-19
In the format string, %Y
is replaced with the four-digit year, %m
is replaced with the two-digit month, and %d
is replaced with the two-digit day.
Note: The strftime()
method returns a string, so you can't use date methods or attributes on the result. If you need to manipulate the date after formatting it, keep a reference to the original date
or datetime
object.
And that's it! You now know how to get today's date in Python and format it in the YYYY-MM-DD
format.
Other Ways to Get Today's Date
While the datetime
module is a powerful tool for working with dates and times in Python, it's not the only way to get today's date. Let's explore some other methods.
One alternative is using the time
module, another built-in Python module for dealing with time. Here's how you can use it to get today's date:
import time
today = time.strftime("%Y-%m-%d")
print(today) # Output: 2023-09-19
When you run this code, you'll get the current date output in the 'YYYY-MM-DD' format. The strftime
method formats the time according to the given format string.
Note: While the time
module can give you the current date, it does not have as many date and time manipulation capabilities as the datetime
module. It's generally better to use datetime
for more complex date and time tasks.
Another option is to use an external library, like pendulum
. Pendulum is a Python library that simplifies and enhances date handling, even beyond what's available in datetime
.
Here's how you can get today's date with pendulum
:
import pendulum
today = pendulum.now().to_date_string()
print(today) # Output: 2023-09-19
This will also give you the current date in the 'YYYY-MM-DD' format. The now
method gets the current date and time, and the to_date_string
method formats it as a string.
Note: Remember that pendulum
is not a built-in module, so you'll need to install it using pip (pip install pendulum
) before you can use it.
Conclusion
We've covered a variety of ways to get today's date in Python and format it as 'YYYY-MM-DD'. We started off with the basics of dates and times in Python, then moved on to getting today's date using the datetime
module. We also looked at how to format the date in the 'YYYY-MM-DD' format. Finally, we explored some other methods for getting the current date, using both the built-in time
module and the external pendulum
library.