I'm trying to generate week number string using Python time module, considering week starts on Sunday.
If my interpretation of the official documentation is correct then this can be achieved by the following code:
import time
time.strftime("%U", time.localtime())
>> 37
My question is, is the above output correct? Shouldn't the output be 38 instead, considering the below details:
My timezone is IST (GMT+5:30)
import time
#Year
time.localtime()[0]
>> 2019
#Month
time.localtime()[1]
>> 9
#Day
time.localtime()[2]
>> 18
Yes, the output is correct. Week 1 started on January 6th, as that was the first Sunday in 2019. January 1st through 5th were week 0:
>>> time.strftime('%U', time.strptime("2019-1-1", "%Y-%m-%d"))
'00'
>>> time.strftime('%U', time.strptime("2019-1-6", "%Y-%m-%d"))
'01'
This is covered in the documentation:
All days in a new year preceding the first Sunday are considered to be in week 0.
You are perhaps looking for the ISO week date, but note that in this system the first day of the week is a Monday.
You can get the week number using that system with the datetime.date.isocalendar() method, or by formatting with %V:
>>> time.strftime("%V", time.localtime())
'38'
>>> from datetime import date
>>> date.today().isocalendar() # returns ISO year, week, and weekday
(2019, 38, 2)
>>> date.today().strftime("%V")
'38'
It's correct since you start counting from the first Sunday.
%U - week number of the current year, starting with the first Sunday as the first day of the first week
https://www.tutorialspoint.com/python/time_strftime.htm
It's correct. Since all days in a new year preceding the first Sunday are considered to be in week 0 (01/01 to 01/05), this week is the week 37.
Related
I'm running Python 3.8.3 and I found something weird about the ISO Week format (%V) :
The first day and the last day of 2019 are both in week 1.
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Output:
2019-W01
2019-W52
2019-W01
Why does it behave like that?
It is fully correct.
As you see in your dates, all of them are in 2019, so it is correct to get 2019 with %Y.
Week number is defined by ISO, and so one week could be considered in previous or in next year.
You need to use %G to get year of the week number (%V).
I have a requirement to run a report on a weekly basis with week starting from Saturday and ending on Friday. However since the datetime or calendar module has week starting from Monday i couldn't use WEEKDAY option.
I tried the below option however it still gives me 5 for saturday,is there any options available to set weekstart day to Saturday so that for saturday it is 0 ? so that i can minus it from current date to get the desired dates or any other solution to acheive the same
Eg) If i run the report on August 30th it should fetch the data for August 18 to August 24
import calendar
calendar.setfirstweekday(calendar.SATURDAY)
calendar.firstweekday()
5
Thanks to the post Python: give start and end of week data from a given date slightly modified it based on my needs
dt = datetime.now()
start = (dt - timedelta(days = (dt.weekday() + 2) % 7)) - timedelta(days=7)
end = (start + timedelta(days=6))
print(start.strftime("%Y-%m-%d"))
print(end.strftime("%Y-%m-%d"))
It gives you 5 for Saturday, because 0 is Monday.
>>> calendar.setfirstweekday(calendar.MONDAY)
>>> calendar.firstweekday()
0
I am trying to get a date based on a number of the week, but there are some annoyances.
The date.weekday() returns the day of the week where 0 in Monday and 6 is Sunday.
The %w directive of date.strftime() and date.strptime() uses the 0 for Sunday and 6 for Saturday.
This causes some really annoying issues when trying to figure out a date given a week number from date.weekday().
Is there a better way of getting a date from a week number?
EDIT:
Added the example.
import datetime
original_date = datetime.date(2014, 8, 24)
week_of_the_date = original_date.isocalendar()[1] # 34
day_of_the_date = original_date.isocalendar()[2] # 7
temp = '{0} {1} {2}'.format(*(2014, week_of_the_date, day_of_the_date-1))
date_from_week = datetime.datetime.strptime(temp, '%Y %W %w')
week_from_new_date = date_from_week.isocalendar()[1] # 35!!
EDIT 2:
I ultimately put the date stuff in the view (using jQuery UI), it has more consistent notions of weeks.
I think the Sunday vs. Monday distinction between weekday and strftime using %W is moot - you could use isoweekday to get those to line up, or %U in strftime if you wanted Sunday as the first day of the week. The real problem is that strftime, based on the underlying C function, determines the first week of the year differently than the ISO definition. With %W the docs say: " All days in a new year preceding the first Monday are considered to be in week 0". ISO calendars count the week containing the first Thursday as the first week, for reasons I do not understand.
Two ways I found to work with ISO weeks, either just getting datetime.date instances back or supporting a variety of operations, are:
this answer with a simple timedelta approach:
What's the best way to find the inverse of datetime.isocalendar()?
this third-party library: https://pypi.python.org/pypi/isoweek/
Date is datetime.date(2013, 12, 30)
I am trying to get week number using
import datetime
datetime.date(2013, 12, 30).isocalendar()[1]
I am getting output as ,
1
Why i am not getting week number of last year , instead i am getting week number of current year?
Whats wrong i am doing here ?
You are doing nothing wrong, 2013/12/30 falls in week 1 of 2014, according to the ISO8601 week numbering standard:
The ISO 8601 definition for week 01 is the week with the year's first Thursday in it.
The Thursday in that week is 2014/01/02.
Other ways to explain the definition, from the same linked WikiPedia article:
It is the first week with a majority (four or more) of its days in January (ISO weeks start on Monday)
Its first day is the Monday nearest to 1 January.
It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.
It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.
If you were looking for the last week number of a given year (52 or 53, depending on the year), I'd use December 28th, which is always guaranteed to be in the last week (because January 4th is always part of the first week of the next year):
def lastweeknumber(year):
return datetime.date(year, 12, 28).isocalendar()[1]
from datetime import date
from datetime import datetime
ndate='10/1/2016'
ndate = datetime.strptime(ndate, '%m/%d/%Y').strftime('%Y,%m,%d')
print('new format:',ndate)
d=ndate.split(',')
wkno = date(int(d[0]),int(d[1]),int(d[2])).isocalendar()[1]
print(wkno)
manually or read a date to a string and get the week number, play around with different formats.
The following code gets the start of the week of a date argument:
weekstart = datetime.datetime.strptime(sys.argv[1], '%m/%d/%Y')
if weekstart.strftime('%A') != 'Sunday':
print "Start date should be a Sunday"
while weekstart.strftime('%A') != 'Sunday':
#weekstart = weekstart - datetime.timedelta(a)
a=a+1
print "Using %s as start date instead" %weekstart.strftime('%m/%d/%Y')
I receive the following error only when the date entered is 7/10/2012, 7/11/2012, 7/12/2012, or 7/13/2012:
while weekstart.strftime('%A') != 'Sunday':
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
What is wrong with this?
Yes, this is a bug in your concept. You are incorrectly incrementing a and decreasing weekstart by that a. So it does not go day by day, but by 1, 2, 3, 4,… days.
7/10/2012 is Tuesday. Substract 1 day…
7/9/2012 is Monday. Substract 2 days…
7/7/2012 is Saturday. Substract 3 days…
7/4/2012 is Wednesday. Substract 4 days…
6/30/2012 is Saturday. Substract 5 days…
6/25/2012 is Monday. Substract 6 days…
6/19/2012 is Tuesday. Substract 7 days, i.e. one week and the loop goes on faster and faster until the year 1899 when it breaks.
It means that if your first day is Tuesday, it will never reach Sunday this way.
It works for 7/11/2012 (and any Wednesday), it breaks for 7/12/2012 (and any Thursday), it breaks for 7/13/2012 (and any Friday), it works for 7/14/2012 (and any Saturday) and it works for 7/16/2012 (and any Monday) and of course it stops immediately for any Sunday.
But it is not what you want.
If you want to get the previous (or today's) Sunday for any day without loops, just use this:
weekstart = weekstart - datetime.timedelta((weekstart.weekday() - 6) % 7)
Or, if you want to avoid mathematics and use libraries (dateutil):
from dateutil import relativedelta
weekstart = weekstart + relativedelta.relativedelta(weekday=relativedelta.SU(-1))