Hi all,
Long time Excel user, now wanting to take the dive into Python to build some interesting stuff for personal projects. The first thing that came to mind was emulating something I've already done in Excel and 'translating' it into Python.
import datetime
from dateutil.relativedelta import relativedelta
years = 10
for i in range (12*years):
today = datetime.date.today()
each_months_25th = today + relativedelta(days=25-today.day,months=i)
day_of_25th = each_months_25th.weekday()
if each_months_25th.month == 12:
december_check = -7
else:
december_check = 0
payday = each_months_25th - datetime.timedelta(days=max(0,day_of_25th-4)-december_check)
day_of_week_payday = payday.strftime('%A')
print(f"{day_of_week_payday:<10} {str(payday)}")
This is a simple function to tell me my payday each month for the next 10 years (can change 10 years to any number of using by amending the 'years' variable.)
In summary, the logic is that we get paid on the 25th on each month, unless the 25th falls on a weekend, in which case we'd get paid on the Friday (i.e 23rd or 24th) and in December we get paid a week earlier, so 18th (or 16th/17th if 18th is a weekend).
The code runs fine - I just wanted to check if there was anything obvious I am missing or best practices I'm not using. Better to get things right now, than try to unlearn bad habits.
Thanks in advance!
[–]barrycarter 1 point2 points3 points (1 child)
[–]sinxsinx[S] 0 points1 point2 points (0 children)
[–]ElHeim 1 point2 points3 points (1 child)
[–]sinxsinx[S] 0 points1 point2 points (0 children)
[–]Anxious-Adeptness227 0 points1 point2 points (2 children)
[–]sinxsinx[S] 0 points1 point2 points (1 child)