Table of Contents
Get First Day of Next Month in Python
This tutorial will demonstrate how to get first day of next month in Python.
Using the datetime.replace()
with datetime.timedelta() function
To get first day of next month in Python:
- Use the
datetime
object to store the current date and time. - Use the
datetime.replace()
anddatetime.timedelta()
function to get first day of next month in Python.
See the code below.
1 2 3 4 5 6 |
import datetime dt = datetime.datetime.now() dt_new = (dt.replace(day=1) + datetime.timedelta(days=32)).replace(day=1) print(dt_new) |
Output:
The datetime
library is used to work with date and time values in Python. The datetime
object can store such values and provide different methods to operate on them. The now()
function returns the current date and time values.
The replace()
function from this library can be used to replace values of different attributes from such objects. The timedelta
object from this library can be used to represent different intervals of time.
In the code example above, we use these methods to get the first day of next month by adding one month to the datetime
value and replacing the value of the day
attribute with one.
Using the calendar.monthrange()
function
To get first day of next month in Python:
- Use the
datetime
object to store the current date and time. - Use the
monthrange()
function to get the days of month. - Use the
replace()
andtimedelta()
functions to get first day of next month in Python.
See the code below.
1 2 3 4 5 6 7 8 |
import datetime from calendar import monthrange days_month = lambda dt: monthrange(dt.year, dt.month)[1] dt = datetime.datetime.now() dt_new = dt.replace(day=1) + datetime.timedelta(days_month(dt)) print(dt_new) |
Output:
The calendar
library also provides an additional set of tools to work with datetime
objects.
The monthrange()
function is used to calculate the first day of month and the days of the month. In the above example, we use the days of the month value and add it to the current date. Then we replace the value of the day
attribute with one.
Further reading:
Using the dateutil.relativedelta
object
To get first day of next month in Python:
- Use the
datetime
object to store the current date and time. - Use the
datetime.relativedelta
object to get first day of next month in Python.
See the code below.
1 2 3 4 5 6 7 |
import datetime from dateutil import relativedelta dt = datetime.datetime.now() dt_new = dt + relativedelta.relativedelta(months=1, day=1) print(dt_new) |
Output:
The dateutil
library provides an additional set of methods and objects to work with datetime
objects. The relativedelta
object is used to replace and add different intervals of time to datetime
objects.
In the above example, we use the relativedelta
object to get first day of next month in Python.
Conclusion
To conclude, we discussed several methods to get first day of next month in Python.
In the first method, we use the replace()
function to replace the value of the day
attribute and add a time interval using the timedelta
object. In the next method, we follow the same logic as method one but we use the monthrange()
function to obtain the value that needs to be added with the timedelta
object.
In the final method, we discussed the dateutil
library and the relativedelta
object from this library to add and replace time values to get the first day of month in Python.