Get First Day of Month in Python

The datetime objects defined in the datetime library provide a convenient way to store date and time values. Other libraries like dateutil also provide extended functionalities that can be used with such objects.

In this tutorial, we will discuss how to get first day of month in Python.

Get First Day of Month in Python

Essentially, we will work with a datetime object that has some specific date and we will try to get the date of the first day of the month from such objects.

Using the datetime.replace() Function

The replace() function from the datetime library can replace the specified values from a datetime object and return a new object with the replaced values. We can specify the new values with parameters like day, month, year, etc to replace the existing values.

To get first day of month in Python using this method, we will replace the day value of the object with one.

For example,

Output:

2022-10-01 00:45:10.636726

In the above example, the today() function returns the current date and time . We replace the day value of this object with one by using the replace() function.

You can also format date to YYYYMMDD in Python.

Using the datetime.strftime() Function

The strftime() function returns the datetime object as a string in our desired format. We can specify the required format within the function.

To get first day of the month in Python, we will pass the 01-%m-%Y format which will return the first day of the month.

See the code below.

Output:

2022-10-01

Using the datetime.timedelta Object

The timedelta object allows us to represent periods of time that can be added or subtracted to datetime objects. We can use such objects to get first day of month in Python.

In this method, we will create a timedelta object containing one day less than the current date. This object will be subtracted from the required datetime object. The result of this operation will be the datetime object with the first day of the month.

See the code below.

Output:

2022-10-01 01:01:21.284710

In the above example, the strftime() function is used to return the day value of the datetime object. The int() function converts this day value to an integer. We return today’s date and subtract a timedelta object with one day less than the current date to return the first day of the month.

Using the arrow Library

The arrow library provides an alternative way to store and work with date and time values. The span attribute returns the first and last attributes over a specific time period.

We can use the month value with this attribute and extract the first element to get first day of month in Python.

For example,

Output:

2022-10-01T00:00:00+00:00

Conclusion

In this article, we discussed several methods to get the first day of month in Python. The most used method involves the use of the replace() function where we replace the day value of a datetime object. The strftime() function works similarly but returns a string as the final output.

The third method involved the use of the timedelta object to subtract some days from the date. We also discussed an alternative library arrow to perform the same efficiently.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *