Monday, October 27, 2025
HomeProgrammingGetting Right now's Date in YYYY-MM-DD in Python

Getting Right now's Date in YYYY-MM-DD in Python


Introduction

Whether or not you are logging occasions or measuring execution occasions, you will usually 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 how you can get as we speak’s date and format it within the YYYY-MM-DD format.

Dates and Occasions in Python

Python supplies the datetime module in its commonplace library for coping with dates and occasions. This module contains a number of lessons for managing dates, occasions, timedeltas, and extra. The 2 lessons 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 supplies a variety of strategies and attributes. The date class, then again, is solely involved with dates (12 months, 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 Right now’s Date in Python

Python’s datetime module supplies a technique referred to as as we speak() that returns the present date and time as a datetime object. Here is how you need to use it:

from datetime import datetime

# get as we speak's date
as we speak = datetime.as we speak()

print(as we speak)  # Output: 2023-09-19 22:17:08.845750

Within the above instance, the as we speak() methodology returned the present date and time. Nonetheless, when you solely want the date, you need to use the date() methodology of a datetime object to get a date object:

from datetime import datetime

# get as we speak's date
as we speak = datetime.as we speak().date()

print(as we speak)  # Output: 2023-09-19

Formatting Date as YYYY-MM-DD

The date and datetime objects present a technique referred to as strftime() that lets you format the date and time in varied methods. The strftime() methodology 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 as we speak's date
as we speak = datetime.as we speak().date()

# format date as YYYY-MM-DD
formatted_date = as we speak.strftime('%Y-%m-%d')

print(formatted_date)  # Output: 2023-09-19

Within the format string, %Y is changed with the four-digit 12 months, %m is changed with the two-digit month, and %d is changed with the two-digit day.

Observe: The strftime() methodology returns a string, so you may’t use date strategies or attributes on the consequence. If that you must manipulate the date after formatting it, maintain a reference to the unique date or datetime object.

And that is it! You now know how you can get as we speak’s date in Python and format it within the YYYY-MM-DD format.

Different Methods to Get Right now’s Date

Whereas the datetime module is a strong device for working with dates and occasions in Python, it isn’t the one technique to get as we speak’s date. Let’s discover another strategies.

One different 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 as we speak’s date:

import time

as we speak = time.strftime("%Y-%m-%d")
print(as we speak) # Output: 2023-09-19

Once you run this code, you will get the present date output within the ‘YYYY-MM-DD’ format. The strftime methodology codecs the time in keeping with the given format string.

Observe: 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 typically higher to make use of datetime for extra advanced date and time duties.

Another 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 out there in datetime.

Here is how one can get as we speak’s date with pendulum:

import pendulum

as we speak = pendulum.now().to_date_string()
print(as we speak) # Output: 2023-09-19

This may also provide the present date within the ‘YYYY-MM-DD’ format. The now methodology will get the present date and time, and the to_date_string methodology codecs it as a string.

Observe: Keep in mind that pendulum will not be 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 as we speak’s date in Python and format it as ‘YYYY-MM-DD’. We began off with the fundamentals of dates and occasions in Python, then moved on to getting as we speak’s date utilizing the datetime module. We additionally checked out how you can 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments