Table of Contents
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,
1 2 3 4 |
from datetime import datetime print(datetime.today().replace(day=1)) |
Output:
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.
1 2 3 4 5 |
from datetime import datetime, timedelta d1 = datetime.today() print(d1.strftime("01-%m-%Y")) |
Output:
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.
1 2 3 4 5 6 |
from datetime import datetime, timedelta d1 = datetime.today() d2 = d1 - timedelta(days = int(d1.strftime("%d"))-1) print(d2) |
Output:
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.
Further reading:
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,
1 2 3 4 5 |
import arrow d =arrow.utcnow().span('month')[0] print(d) |
Output:
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.