By this I am getting current week number
datetime.date(2014, 9, 30).isocalendar()[1]
but I want to get what is next ISO week number and max ISO week number in current year in python.
I can't add current week number + 1 to get next week number, because it may be that year doesn't have that week number.
To get next week's week number, add a timedelta:
>>> import datetime
>>> today = datetime.date(2014, 9, 30)
>>> next_week = today + datetime.timedelta(days=7)
>>> next_week.isocalendar()[1]
41
To get the last week number in the year, note that the following rule is used:
The following years have 53 weeks:
years starting with Thursday
leap years starting with Wednesday
Related
I use deltatime to subtract 12 weeks and get the exact date
but from this date I have to recover on Friday. this means that if we subtract 12 weeks I have Monday January 3, I have to make a change to get Friday January 7.
Code exemple :
I use this code to get the initial date :
week_num_period = datetime.today()- timedelta(weeks=12)
week_start_perioddddd = week_num_period.strftime("%Y-%m-%d")
but from that date i need to get the friday of that week !
i tried with
week_start_period = next_day(week_start_perioddddd, 'Friday')
week_start_period
but next_day doesn't apply on str !
could someone help me ?
So you want to get the formatted date for the Friday from the week 12 weeks ago?
Add the timedelta of: 4 (weekday index of Friday) minus the weekday index of the day you calculated.
>>> week_num_period = datetime.today()- timedelta(weeks=12)
>>> week_num_period.date()
datetime.date(2022, 1, 6)
>>> friday = week_num_period + timedelta(days=4 - week_num_period.weekday())
>>> friday.date()
datetime.date(2022, 1, 7)
>>> week_start_period = friday.strftime("%Y-%m-%d")
>>> week_start_period
'2022-01-07'
>>>
This question already has answers here:
how to calculate 38 days from next month starting in Python
(4 answers)
Closed 1 year ago.
let us consider date as
invoice.created_at = datetime.date(2021, 11, 17)
next_month_first_date here is getting the nextmonth first date
next_month_first_date = (invoice.created_at.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)
# datetime.date(2021, 12, 1)
Now I need last day of invoice.created_at month
this_month_last_day = ?
how to find last date of invoice.created_at month i.e 30/11/2021 and calculate 38 days from this_month_last_day? 38 days from this_month_last_day is 7/01/2022
Since you already have the 1st day of the next month you can simply subtract 1 day from this to get the last day of the current month:
this_month_last_day = next_month_first_date - datetime.timedelta(days=1)
>>> this_month_last_day + datetime.timedelta(days=38)
datetime.date(2022, 1, 7)
You can use Python's calender module to get the last day of the month
import calendar
import datetime
created_at = datetime.date(2021, 11, 17)
last_day_as_int = calendar.monthrange(created_at.year, created_at.month)[1] # 30
last_day_as_date = created_at.replace(day=last_day_as_int) # datetime.date(2021, 11, 30)
target_date = last_day_as_date + datetime.timedelta(days=38) # datetime.date(2022, 1, 7)
This question is related to Get date from week number, and is possibly a duplicate of the latter, however, I think what is suggested in the accepted answer to that question does not really work.
In [6]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w")
Out[6]: datetime.datetime(2019, 5, 6, 0, 0)
Notice how it returns Monday 2019-5-6. However, according to the calendar (I use http://whatweekisit.org for reference), 2019-5-6 the Monday of week 19.
Similarly, the example provided in the original question:
In [7]: datetime.datetime.strptime('2013-26-1', "%Y-%W-%w")
Out[7]: datetime.datetime(2013, 7, 1, 0, 0)
According to http://whatweekisit.org/calendar-2013.html 2013-7-1 is the Monday of week 27.
Also
In [8]: datetime.datetime.strptime('2019-18-1', "%Y-%W-%w").isocalendar()[1]
Out[8]: 19
Notice how I give week 18 to strptime, and get week 19 back from isocalendar.
I am completely lost and would very much appreciate if someone could explain what is going on here. My original goal though is to get week start date from week number.
Based off of my testing, datetime does not consider the first week of 2019 (i.e. Jan 1-Jan 6) as week 1 because it isn't a full week; December 31st, 2018 is part of the week but is not in 2019. I suppose you'll have to accomodate for that by checking the output of datetime.datetime.strptime('year-1-1', "%Y-%W-%w") == datetime.datetime.strptime('year-0-1', "%Y-%W-%w"). If false, subtract 1.
2018 is an example of a year where datetime does return the same value as isocalendar because the first Monday of the year is Jan 1.
From the isocalendar docs:
The ISO year consists of 52 or 53 full weeks, and where a week starts on a Monday and ends on a Sunday. The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday. This is called week number 1, and the ISO year of that Thursday is the same as its Gregorian year.
On the other hand, strptime starts from the first full week, in fact, since 2019 starts from Tuesday, they start from different weeks:
import datetime as dt
strp_first = dt.datetime.strptime('2019-1-1', "%Y-%W-%w")
>>> print(strp_first)
2019-01-07 00:00:00
>>> print(strp_first.isocalendar()[1])
2
While in 2021, which starts from Friday:
strp_first = dt.datetime.strptime('2021-1-1', "%Y-%W-%w")
>>> print(strp_first)
2021-01-04 00:00:00
>>> print(strp_first.isocalendar()[1])
1
Is there a workaround for the following
from datetime import datetime, timedelta
a = datetime.now() # <== 2016-03-09 11:06:04.824047
print a.strftime("%U")
>>> 10
#go back to previous Sunday
b = a - timedelta(days = 3)
print b.strftime("%U")
>>> 10
print b.weekday()
>>> 6
Shouldn't b.strftime("%U") be 9 as it the last day of the week?
%U treats weeks as starting on Sunday, not on Monday. From the documentation:
%U
Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.
You could use the %W format, which gives a zero-padded week number based on Monday being the first day of the week:
%W
Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.
The alternative is to use b.isocalendar()[1] if you need to get the week number as per ISO 8601. The rules for what is considered week 1 differ from the ISO calendar calculations; %W bases this on the first Monday in the year, while ISO 8601 states that the week that includes January 4 is the first week. For 2016 both systems align, but that's not the case in 2014, 2015 or 2019:
>>> d = datetime(2019, 3, 9)
>>> d.strftime('%W')
'09'
>>> d.isocalendar()[1]
10
If you wait until Python 3.6, you can use the %V format to include the ISO week number, see issue 12006.
datetime.date(2010, 1, 1).isocalendar()[1]
gives week number 53 since 1st Jan 2010 was in a week which started in 2009. However, I would like to start Jan 1, 2010 as week 1. Is there any option in datetime isocalendar to do this?
If I follow the solution at How can I get the current week using Python?, I get 53 as result
Compute the number of days since the first of the year, integer-divide by 7 and add 1:
>>> import datetime as DT
>>> (DT.date(2010,1,1)-DT.date(2010,1,1)).days // 7 + 1
1
This notion of week number is very different from the ISO week definition, so you aren't going to find an option to do this in isocalendar.
If you're going to be getting data in total weeks since a point, you can mod that number by the number of weeks in a year (52) to get the remainder (ie, how many weeks into the year you are):
>>> 53 % 52
>>> 1
>>> 925 % 52
>>> 41