Wednesday, September 20, 2023
HomeProgrammingGetting In the present day's Date in YYYY-MM-DD in Python

Getting In the present day's Date in YYYY-MM-DD in Python


Introduction

Whether or not you are logging occasions or measuring execution occasions, you will typically end up working with dates. In Python, the built-in datetime module makes it simple to get the present date and time, format it, and even do time math. On this Byte, we’ll give attention to easy methods to get in the present day’s date and format it within the YYYY-MM-DD format.

Dates and Occasions in Python

Python gives the datetime module in its normal library for coping with dates and occasions. This module consists of 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 gives 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 In the present day’s Date in Python

Python’s datetime module gives a technique referred to as in the present day() that returns the present date and time as a datetime object. Here is how you should utilize it:

from datetime import datetime

# get in the present day's date
in the present day = datetime.in the present day()

print(in the present day)  # Output: 2023-09-19 22:17:08.845750

Within the above instance, the in the present day() methodology returned the present date and time. Nevertheless, should you solely want the date, you should utilize the date() methodology of a datetime object to get a date object:

from datetime import datetime

# get in the present day's date
in the present day = datetime.in the present day().date()

print(in the present day)  # 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() methodology takes a format string the place every %-prefixed character is changed with knowledge from the date and time.

To format a date within the YYYY-MM-DD format, you should utilize the %Y, %m, and %d format codes:

from datetime import datetime

# get in the present day's date
in the present day = datetime.in the present day().date()

# format date as YYYY-MM-DD
formatted_date = in the present day.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.

Observe: The strftime() methodology returns a string, so you’ll be able to’t use date strategies or attributes on the consequence. If you might 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 easy methods to get in the present day’s date in Python and format it within the YYYY-MM-DD format.

Different Methods to Get In the present day’s Date

Whereas the datetime module is a strong device for working with dates and occasions in Python, it isn’t the one approach to get in the present day’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 should utilize it to get in the present day’s date:

import time

in the present day = time.strftime("%Y-%m-%d")
print(in the present day) # 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 line 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 usually higher to make use of datetime for extra advanced 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 out there in datetime.

Here is how one can get in the present day’s date with pendulum:

import pendulum

in the present day = pendulum.now().to_date_string()
print(in the present day) # Output: 2023-09-19

This will even 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: Do not forget 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 should utilize it.

Conclusion

We have lined quite a lot of methods to get in the present day’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 in the present day’s date utilizing the datetime module. We additionally checked out easy methods 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.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments