I'm trying to find the first day of the month in python with one condition: if my current date passed the 25th of the month, then the first date variable will hold the first date of the next month instead of the current month. I'm doing the following: Show
is there a cleaner way for doing this?
Mel 5,56810 gold badges39 silver badges42 bronze badges asked May 23, 2016 at 16:42
2 Can be done on the same line using
cglacet 7,3003 gold badges39 silver badges53 bronze badges answered Jul 23, 2017 at 15:40
5 This is a pithy solution.
One thing to note with the original code example is that using answered May 23, 2016 at 17:05
andrewandrew 3,8291 gold badge23 silver badges38 bronze badges 1 Use dateutil.
answered May 23, 2016 at 16:55
lampslavelampslave 1,32514 silver badges18 bronze badges 1
answered Dec 13, 2018 at 7:24
Balaji.J.BBalaji.J.B 5386 silver badges14 bronze badges 1 Use arrow.
answered Jul 17, 2018 at 10:03
ImPerat0R_ImPerat0R_ 5538 silver badges8 bronze badges This could be an alternative to Gustavo Eduardo Belduma's answer:
answered Apr 9, 2020 at 7:54
tjurkantjurkan 3614 silver badges7 bronze badges 1 Yes, first set a datetime to the start of the current month. Second test if current date day > 25 and get a true/false on that. If True then add add one month to the start of month datetime object. If false then use the datetime object with the value set to the beginning of the month.
answered May 23, 2016 at 16:56
mba12mba12 2,5326 gold badges34 silver badges52 bronze badges The arrow module will steer you around and away from subtle mistakes, and it's easier to use that older products.
In this case there is no need for you to consider the varying lengths of months, for instance. Here's the output from this script.
answered Feb 22, 2017 at 14:41
Bill BellBill Bell 20.4k5 gold badges42 silver badges57 bronze badges 1 I found a clean way to do this is to create a datetime object using the month and year attributes of todayDate, with days set to 1 i.e.
answered Sep 24, 2021 at 11:05
You can use dateutil.rrule:
answered Sep 21, 2018 at 8:06
dtatarkindtatarkin 1,0217 silver badges6 bronze badges One-liner:
answered Dec 7, 2021 at 10:32
My solution to find the first and last day of the current month:
answered Oct 22, 2019 at 10:17
mhyousefimhyousefi 9042 gold badges12 silver badges29 bronze badges First day of next month:
Then we replace the month and the day
answered Jul 22, 2020 at 15:40
Inspired by Jouberto's and @akx's answers (elsewhere), oneliners without any dependencies:
answered Sep 5 at 12:53
Aapo RistaAapo Rista 1111 silver badge4 bronze badges How do I get the first day of the month in Python?Get First Day of Month Using Python. import datetime currentDate = datetime. date. today() firstDayOfMonth = datetime. date(currentDate. ... . import datetime currentDate = datetime. date. today() firstDayOfMonth = datetime. ... . import calendar import datetime currentDate = datetime. date. today() lastDayOfMonth = datetime.. How do I get start and end date month data from a specific date in Python?You can use current_date. replace(day=1) to get first day in current month. And if you substract datetime. timedelta(days=1) then you get last day in previous month.
How do I get the first day of the last month in Python?python first day of last month. from datetime import date, timedelta.. last_day_of_prev_month = date. today(). replace(day=1) - timedelta(days=1). start_day_of_prev_month = date. today(). ... . print("First day of prev month:", start_day_of_prev_month). print("Last day of prev month:", last_day_of_prev_month). How do I get the start date and end date in Python?“how to use a date range in python from start date and end date” Code Answer. import pandas as pd.. from datetime import datetime.. pd. date_range(end = datetime. today(), periods = 100). to_pydatetime(). tolist(). pd. date_range(start="2018-09-09",end="2020-02-02"). to_pydatetime(). tolist(). |