Go to december month on Python - python

I'm trying to go back December month but my results are month 0 instead of month 12... how can I do that?
I create a code and on it I need the Today date and also the last day of the before month, I mean:
for example today is 25/01/2018 and I need 31/12/2017. On February I'm going to run it again and I need for example 25/02/2018 and 31/01/2018. That is the relationship I'm looking for
import datetime
fecha17 = time.strftime(str((int(time.strftime("%d")))-1) +"/" + str((int(time.strftime("%m")))-1) + "/%Y")

Related

How to get date range for a specific month starting with a previous month using Pandas

I'm trying to build a list of "pay days" for a given month in the future knowing only when the pay days started months ago. For example:
Starting date - When the paychecks started: 1/6/2023
Frequency is every two weeks
So if I want to know which dates are pay days in March, I have to start at the 1/6/2023 and add two weeks until I get to March to know that the first pay day in March is 3/3/2/2023.
Then I want my final list of dates to be only those March dates of:
(3/3/2023, 3/17/2023, 3/31/2023)
I know I can use pandas to do something like:
pd.date_range(starting_date, starting_date+relativedelta(months=1), freq='14d')
but it would include every date back to 1/6/2023.
The easiest thing to do here would be to just update the starting_date parameter to be the first pay day in the month you're interested in.
To do this, you can use this function that finds the first pay day in a given month by first finding the difference between your start date and the desired month.
# month is the number of the month (1-12)
def get_first_pay_day_in_month(month=datetime.datetime.now().month,
year=datetime.datetime.now().year,
start_date=datetime.datetime(2023, 1, 6),
):
diff = datetime.datetime(year, month, 1) - start_date
freq = 14
if diff.days % freq == 0:
print(f'Difference: {diff.days/freq} weeks')
return datetime.datetime(year,month,1)
else:
print(f'Difference: {diff.days} days')
print(f'Days: {diff.days % freq} extra')
return datetime.datetime(year,month,1 + 14 - (diff.days % freq))
Then you can use this function to get the first pay day of a specific month and plug it into the date_range method.
from dateutil import relativedelta
starting_date = get_first_pay_day_in_month(month=3)
pay_days = pd.date_range(starting_date, starting_date+relativedelta.relativedelta(months=1), freq='14d')
print(pay_days)

How to find the last saturday of current month in Python

I need some help with finding the last saturday of current month in Python. Now, this code run today show me Saturday 31th July, but I want to obtain Saturday 28th August.
off= (date.today().weekday() - 5)%7
Last_Saturday = (date.today() - timedelta(days=off)).strftime("%d/%m/%Y")
Adapted from this question:
Python: get last Monday of July 2010
You could do this to get the day of the month number, then do whatever you want with that
from datetime import datetime
import calendar
month = calendar.monthcalendar(datetime.now().year, datetime.now().month)
day_of_month = max(month[-1][calendar.SATURDAY], month[-2][calendar.SATURDAY])
Using #quamrana answer you could do the following
import calendar
if calendar.monthcalendar(2021, 8)[-1][5] != 0:
print(calendar.monthcalendar(2021, 8)[-1][5])
else:
print(calendar.monthcalendar(2021, 8)[-2][5])
Output
>>> if calendar.monthcalendar(2021, 8)[-1][5] != 0:
... print(calendar.monthcalendar(2021, 8)[-1][5])
... else:
... print(calendar.monthcalendar(2021, 8)[-2][5])
...
28
>>>
In whatever month we are, you can use this code to get the date corresponding to the last Saturday.
Note that I considered only the last week of the month.
Inspiration came from Percentage of Wednesdays ending months in the 21st Century
from datetime import date, datetime
import calendar as cal
today = date.today()
month = today.month
year = today.year
length_month = cal.monthrange(year, month)[1]
for day in range(length_month,length_month-7,-1):
if datetime(year, month, day).weekday()==5:
last_saturday = datetime(year, month, day).strftime("%d/%m/%Y")
print(f'Last Saturday of the current month : {last_saturday}')

Python: get first date of the week from calender week and year (working for 2020 but not for 2021)

I would like to extract the first date of the week from the year and the calender week (in Europe where the first calender week is the week that includes the 4th of January)
My code works correctly for
import datetime
year=20
week=53
print(datetime.datetime.strptime(str(year) + "-"+ str(week-1) +'-1-CET', "%y-%U-%w-%Z"))
the output is 2020-12-28 00:00:00 which is correct
for
import datetime
year=21
week=1
print(datetime.datetime.strptime(str(year) + "-"+ str(week-1) +'-1-CET', "%y-%U-%w-%Z"))
I get also 2020-12-28 00:00:00.The correct output would be 2021-01-04.
Could you please tell me where my mistake is?
Thanks
Using %V instead of %U should make things easier.

Get first and last day of given week number in python

I need a function which returns the first and last day respectively the Monday and Sunday of a given week number (and a year).
There is a difficulty for weeks where Jan 1st is not a Monday so I cannot use the standard datetime.datetime.strptime().
Here's the solution:
import calendar
import datetime
from datetime import timedelta
def get_start_and_end_date_from_calendar_week(year, calendar_week):
monday = datetime.datetime.strptime(f'{year}-{calendar_week}-1', "%Y-%W-%w").date()
return monday, monday + datetime.timedelta(days=6.9)
I extended the great logic of this post.
Update
There is a pitfall with the first calendar week. Certain countries handle the first week number differently. For example in Germany, if the first week in January has less than 4 days, it is counted as the last week of the year before. There's an overview at Wikipedia.
This other version found here is flawless and simple: https://www.pythonprogramming.in/how-to-get-start-and-end-of-week-data-from-a-given-date.html
##
# Python's program to get start and end of week
from datetime import datetime, timedelta
date_str = '2018-01-14'
date_obj = datetime.strptime(date_str, '%Y-%m-%d')
start_of_week = date_obj - timedelta(days=date_obj.weekday()) # Monday
end_of_week = start_of_week + timedelta(days=6) # Sunday
print(start_of_week)
print(end_of_week)

How to determine the date of Starting day of current and last week in python ?

I need to find the date of the starter day of current and last week.
Here, business day of a week starts from SUNDAY. So, for today the date I want is to be "07-16-2017" as "mm--dd--yyyy" format. I can get today's date easily from datetime.datetime.now().strftime ("%Y-%m-%d") but from the sysdate I have to pull out the starter day of the week.
I need the starter day's date for last week as well.
There is no default method for week information in datetime. Is there any other method in any package in python to determine the required information ?
You can use calendar.firstweekday() to check what the first day of the week is on that computer (0 is Monday, 6 is Sunday).
1) Let's say that firstweekday returned 1 (Sunday). Then you can use
date.today() - datetime.timedelta(days=date.today().isoweekday() % 7)
to compute the date of the last Sunday.
2) Let's say that firstweekday returned 0 (Monday).
date.today() - datetime.timedelta(days=date.today().isoweekday() % 7 - 1)
to compute the date of the last Monday.
Hope this gives you some direction.

Categories

Resources