Python:
Getting the current date
How to:
Using the standard library datetime
:
The datetime
module in Python’s standard library provides classes for manipulating dates and times. To get the current date, you can use the date.today()
method.
from datetime import date
today = date.today()
print(today) # Output: YYYY-MM-DD (e.g., 2023-04-05)
Time Formatting:
If you require the current date in a different format, the strftime
method allows you to specify custom date formatting:
from datetime import date
today = date.today()
formatted_date = today.strftime('%B %d, %Y') # Example format: "April 05, 2023"
print(formatted_date)
Using pendulum
for more flexibility (a popular third-party library):
Pendulum
is a third-party library that offers a more intuitive approach to dealing with dates and times in Python. It extends the standard datetime functionalities and simplifies time zone management, among other features.
First, ensure you’ve installed pendulum
via pip:
pip install pendulum
Then, to get the current date:
import pendulum
today = pendulum.now().date()
print(today) # Output: YYYY-MM-DD (e.g., 2023-04-05)
With pendulum
, formatting is also straightforward and similar to the strftime
approach:
import pendulum
today = pendulum.now()
formatted_date = today.to_formatted_date_string() # Default format: "Apr 5, 2023"
print(formatted_date)