Reverse of datetime.weekday()? - python

d : Datetime object
Given a date d and a day of the week x in the range of 0–6, return the date of x within the same week as d.
I can think of some ways to do this, but they all seem rather inefficient. Is there a pythonic way?
Example
Input: datetime(2020,2,4,18,0,55,00000), 6
Output: date(2020,2,7)
Input: datetime(2020,2,4,18,0,55,00000), 0
Output date(2020,2,3)

This approach gets the first day in the week and goes from there to find the date requested by the weekday integer:
import datetime as dt
def weekday_in_week(d,weekday=None):
if not weekday:
return None
week_start = d - dt.timedelta(days=d.weekday())
return week_start + dt.timedelta(days=weekday)
Example usage:
In [27]: weekday_in_week(dt.date.today(),6)
Out[27]: datetime.date(2020, 2, 9)
Remember that the weekdays are as such: 0 is Monday, 6 is Sunday.

Related

Calendar start and end of the week in Python [duplicate]

Using Python...
How can I select all of the Sundays (or any day for that matter) in a year?
[ '01/03/2010','01/10/2010','01/17/2010','01/24/2010', ...]
These dates represent the Sundays for 2010. This could also apply to any day of the week I suppose.
You can use date from the datetime module to find the first Sunday in a year and then keep adding seven days, generating new Sundays:
from datetime import date, timedelta
def allsundays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 6 - d.weekday()) # First Sunday
while d.year == year:
yield d
d += timedelta(days = 7)
for d in allsundays(2010):
print(d)
Pandas has great functionality for this purpose with its date_range() function.
The result is a pandas DatetimeIndex, but can be converted to a list easily.
import pandas as pd
def allsundays(year):
return pd.date_range(start=str(year), end=str(year+1),
freq='W-SUN').strftime('%m/%d/%Y').tolist()
allsundays(2017)[:5] # First 5 Sundays of 2017
# ['01/01/2017', '01/08/2017', '01/15/2017', '01/22/2017', '01/29/2017']
Using the dateutil module, you could generate the list this way:
#!/usr/bin/env python
import dateutil.relativedelta as relativedelta
import dateutil.rrule as rrule
import datetime
year=2010
before=datetime.datetime(year,1,1)
after=datetime.datetime(year,12,31)
rr = rrule.rrule(rrule.WEEKLY,byweekday=relativedelta.SU,dtstart=before)
print rr.between(before,after,inc=True)
Although finding all Sundays is not too hard to do without dateutil, the module is handy especially if you have more complicated or varied date calculations.
If you are using Debian/Ubuntu, dateutil is provided by the python-dateutil package.
from datetime import date, timedelta
from typing import List
def find_sundays_between(start: date, end: date) -> List[date]:
total_days: int = (end - start).days + 1
sunday: int = 6
all_days = [start + timedelta(days=day) for day in range(total_days)]
return [day for day in all_days if day.weekday() is sunday]
date_start: date = date(2018, 1, 1)
date_end: date = date(2018, 12, 31)
sundays = find_sundays_between(date_start, date_end)
If looking for a more general approach (ie not only Sundays), we can build on sth's answer:
def weeknum(dayname):
if dayname == 'Monday': return 0
if dayname == 'Tuesday': return 1
if dayname == 'Wednesday':return 2
if dayname == 'Thursday': return 3
if dayname == 'Friday': return 4
if dayname == 'Saturday': return 5
if dayname == 'Sunday': return 6
This will translate the name of the day into an int.
Then do:
from datetime import date, timedelta
def alldays(year, whichDayYouWant):
d = date(year, 1, 1)
d += timedelta(days = (weeknum(whichDayYouWant) - d.weekday()) % 7)
while d.year == year:
yield d
d += timedelta(days = 7)
for d in alldays(2020,'Sunday'):
print(d)
Note the presence of % 7 in alldays(). This outputs:
2020-01-05
2020-01-12
2020-01-19
2020-01-26
2020-02-02
2020-02-09
2020-02-16
...
Can also do:
for d in alldays(2020,'Friday'):
print(d)
which will give you:
2020-01-03
2020-01-10
2020-01-17
2020-01-24
2020-01-31
2020-02-07
2020-02-14
...
You can iterate over a calendar for that year.
The below should return all Tuesdays and Thursdays for a given year.
# Returns all Tuesdays and Thursdays of a given year
from datetime import date
import calendar
year = 2016
c = calendar.TextCalendar(calendar.SUNDAY)
for m in range(1,13):
for i in c.itermonthdays(year,m):
if i != 0: #calendar constructs months with leading zeros (days belongng to the previous month)
day = date(year,m,i)
if day.weekday() == 1 or day.weekday() == 3: #if its Tuesday or Thursday
print "%s-%s-%s" % (year,m,i)
import time
from datetime import timedelta, datetime
first_date = '2021-01-01'
final_date = '2021-12-31'
first_date = datetime.strptime(first_date, '%Y-%m-%d')
last_date = datetime.strptime(final_date, '%Y-%m-%d')
week_day = 'Sunday'
dates = [first_date + timedelta(days=x) for x in range((last_date - first_date).days + 1) if (first_date + timedelta(days=x)).weekday() == time.strptime(week_day, '%A').tm_wday]
It will return all Sunday date of given date range.
Here's a complete generator function that builds on the solution from #sth. It includes the crucial fix that was mentioned in his solution's comments.
You can specify the day of week (using Python's indexing with 0=Monday to 6=Sunday), the starting date, and the number of weeks to enumerate.
def get_all_dates_of_day_of_week_in_year(day_of_week, start_year, start_month,
start_day, max_weeks=None):
'''
Generator function to enumerate all calendar dates for a specific day
of the week during one year. For example, all Wednesdays in 2018 are:
1/3/2018, 1/10/2018, 1/17/2018, 1/24/2018, 1/31/2018, 2/7/2018, etc.
Parameters:
----------
day_of_week : int
The day_of_week should be one of these values: 0=Monday, 1=Tuesday,
2=Wednesday, 3=Thursday, 4=Friday, 5=Saturday, 6=Sunday.
start_year : int
start_month : int
start_day : int
The starting date from which to list out all the dates
max_weeks : int or None
If None, then list out all dates for the rest of the year.
Otherwise, end the list after max_weeks number of weeks.
'''
if day_of_week < 0 or day_of_week > 6:
raise ValueError('day_of_week should be in [0, 6]')
date_iter = date(start_year, start_month, start_day)
# First desired day_of_week
date_iter += timedelta(days=(day_of_week - date_iter.weekday() + 7) % 7)
week = 1
while date_iter.year == start_year:
yield date_iter
date_iter += timedelta(days=7)
if max_weeks is not None:
week += 1
if week > max_weeks:
break
Example usage to get all Wednesdays starting on January 1, 2018, for 10 weeks.
import calendar
day_of_week = 2
max_weeks = 10
for d in get_all_dates_of_day_of_week_in_year (day_of_week, 2018, 1, 1, max_weeks):
print "%s, %d/%d/%d" % (calendar.day_name[d.weekday()], d.year, d.month, d.day)
The above code produces:
Wednesday, 2018/1/3
Wednesday, 2018/1/10
Wednesday, 2018/1/17
Wednesday, 2018/1/24
Wednesday, 2018/1/31
Wednesday, 2018/2/7
Wednesday, 2018/2/14
Wednesday, 2018/2/21
Wednesday, 2018/2/28
Wednesday, 2018/3/7
according to #sth answer I like to give you an alternative without a function
from datetime import date, timedelta,datetime
sunndays = list()
year_var = datetime.now() #get current date
year_var = year_var.year #get only the year
d = date(year_var, 1, 1) #get the 01.01 of the current year = 01.01.2020
#now we have to skip 4 days to get to sunday.
#d.weekday is wednesday so it has a value of 2
d += timedelta(days=6 - d.weekday()) # 01.01.2020 + 4 days (6-2=4)
sunndays.append(str(d.strftime('%d-%m-%Y'))) #you need to catch the first sunday
#here you get every other sundays
while d.year == year_var:
d += timedelta(days=7)
sunndays.append(str(d.strftime('%d-%m-%Y')))
print(sunndays) # only for control
if you want every monday for example
#for 2021 the 01.01 is a friday the value is 4
#we need to skip 3 days 7-4 = 3
d += timedelta(days=7 - d.weekday())
according to #sth answer,it will lost the day when 1st is sunday.This will be better:
d = datetime.date(year, month-1, 28)
for _ in range(5):
d = d + datetime.timedelta(days=-d.weekday(), weeks=1)
if d.month!=month:
break
date.append(d)

How to set a get the date of the day of the week?

Basically what i want is to have the day of the week's date after saying next week or similar format and i though i found what i need in here:
Find the date for the first Monday after a given a date
However testing the code proved it's giving somewhat answers i'd want differently :
import datetime
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
return d + datetime.timedelta(days_ahead)
d = datetime.date(2017, 11, 30)
next_monday = next_weekday(d, 0) # 0 = Monday, 1=Tuesday, 2=Wednesday...
print(next_monday)
It worked as expected to give the date correctly for next_weekday(d,0), 1,2,3 then for next_weekday(d,4) i get 2017-12-01 which my system interprets as faulty because this friday equate 2017-12-01 while next friday equates 2017-12-08 same for saturday and Sunday, so basically what i want if the day of the week we're seeking is still in the same week to give the date of that day for the week after.
Could you not just always add 7 to days_ahead rather than only when the
"Target day has already happend this week" to get a day in next week every time:
import datetime
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday() + 7
return d + datetime.timedelta(days_ahead)
d = datetime.date(2017, 11, 30)
next_friday = next_weekday(d, 4)
print(next_friday) # 2017-12-08 which is next week rather than the friday this week: 2017-12-01

Python get number of the week by month

Any one could help me please, How to get number of week by month in Python?
from datetime import datetime, date, timedelta
Input:
date1 = "2015-07-09"
date2 = "2016-08-20"
Output:
2015-07 : 4
2015-08 : 5
2015-08 : 4
....
2016-08 : 5
How to count number of the week by monthly from date1 to date2?
If you wanted to measure the number of full weeks between two dates, you could accomplish this with datetime.strptime and timedelta like so:
from datetime import datetime, date, timedelta
dateformat = "%Y-%m-%d"
date1 = datetime.strptime("2015-07-09", dateformat)
date2 = datetime.strptime("2016-08-20", dateformat)
weeks = int((date2-date1).days/7)
print weeks
This outputs 58. The divide by 7 causes the number of weeks to be returned. The number of whole weeks is used (rather than partial) because of int which returns only the integer portion. If you wanted to get the number of partial weeks, you could divide by 7.0 instead of 7, and ensure that you remove the int piece.
Try this:
date1 = "2015-07-09"
date2 = "2016-08-20"
d1 = datetime.datetime.strptime(date1, '%Y-%m-%d').date()
d2 = datetime.datetime.strptime(date2, '%Y-%m-%d').date()
diff = d2 -d1
weeks, days = divmod(diff.days, 7)

How to get week start dates and week number of each week in a year considering start day of the week is Monday in python?

How can I get week start dates of each week in a year, considering start day of the week is Monday in python?
This assumes start day is Sunday:
>>>import datetime as datetime
>>>dt = datetime .date(2013,12,30)
>>>dt.isocalendar()[1]
1
However, result shouldn't be 1, because 30-12-2013 is still in 2013.
I don't think behaviour of isocalendar can be changed.
From : http://docs.python.org/2/library/datetime.html#datetime.date.isocalendar
The first week of an ISO year is the first (Gregorian) calendar week of a year containing a Thursday.
But strftime can display week number :
%U "All days in a new year preceding the first Sunday are considered to be in week 0."
%W "All days in a new year preceding the first Monday are considered to be in week 0."
>>> import datetime
>>> dt = datetime.date(2013,12,30)
>>> dt.isocalendar()
(2014, 1, 1)
>>> dt.strftime("%U")
'52'
But to respond to your first question, why don't you just use datetime.timedelta ?
>>> import datetime
>>> dt = datetime.date(2013,12,30)
>>> w = datetime.timedelta(weeks=1)
>>> dt - w
datetime.date(2013, 12, 23)
>>> dt + w
datetime.date(2014, 1, 6)
>>> dt + 10 * w
datetime.date(2014, 3, 10)

How can I select all of the Sundays for a year using Python?

Using Python...
How can I select all of the Sundays (or any day for that matter) in a year?
[ '01/03/2010','01/10/2010','01/17/2010','01/24/2010', ...]
These dates represent the Sundays for 2010. This could also apply to any day of the week I suppose.
You can use date from the datetime module to find the first Sunday in a year and then keep adding seven days, generating new Sundays:
from datetime import date, timedelta
def allsundays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 6 - d.weekday()) # First Sunday
while d.year == year:
yield d
d += timedelta(days = 7)
for d in allsundays(2010):
print(d)
Pandas has great functionality for this purpose with its date_range() function.
The result is a pandas DatetimeIndex, but can be converted to a list easily.
import pandas as pd
def allsundays(year):
return pd.date_range(start=str(year), end=str(year+1),
freq='W-SUN').strftime('%m/%d/%Y').tolist()
allsundays(2017)[:5] # First 5 Sundays of 2017
# ['01/01/2017', '01/08/2017', '01/15/2017', '01/22/2017', '01/29/2017']
Using the dateutil module, you could generate the list this way:
#!/usr/bin/env python
import dateutil.relativedelta as relativedelta
import dateutil.rrule as rrule
import datetime
year=2010
before=datetime.datetime(year,1,1)
after=datetime.datetime(year,12,31)
rr = rrule.rrule(rrule.WEEKLY,byweekday=relativedelta.SU,dtstart=before)
print rr.between(before,after,inc=True)
Although finding all Sundays is not too hard to do without dateutil, the module is handy especially if you have more complicated or varied date calculations.
If you are using Debian/Ubuntu, dateutil is provided by the python-dateutil package.
from datetime import date, timedelta
from typing import List
def find_sundays_between(start: date, end: date) -> List[date]:
total_days: int = (end - start).days + 1
sunday: int = 6
all_days = [start + timedelta(days=day) for day in range(total_days)]
return [day for day in all_days if day.weekday() is sunday]
date_start: date = date(2018, 1, 1)
date_end: date = date(2018, 12, 31)
sundays = find_sundays_between(date_start, date_end)
If looking for a more general approach (ie not only Sundays), we can build on sth's answer:
def weeknum(dayname):
if dayname == 'Monday': return 0
if dayname == 'Tuesday': return 1
if dayname == 'Wednesday':return 2
if dayname == 'Thursday': return 3
if dayname == 'Friday': return 4
if dayname == 'Saturday': return 5
if dayname == 'Sunday': return 6
This will translate the name of the day into an int.
Then do:
from datetime import date, timedelta
def alldays(year, whichDayYouWant):
d = date(year, 1, 1)
d += timedelta(days = (weeknum(whichDayYouWant) - d.weekday()) % 7)
while d.year == year:
yield d
d += timedelta(days = 7)
for d in alldays(2020,'Sunday'):
print(d)
Note the presence of % 7 in alldays(). This outputs:
2020-01-05
2020-01-12
2020-01-19
2020-01-26
2020-02-02
2020-02-09
2020-02-16
...
Can also do:
for d in alldays(2020,'Friday'):
print(d)
which will give you:
2020-01-03
2020-01-10
2020-01-17
2020-01-24
2020-01-31
2020-02-07
2020-02-14
...
You can iterate over a calendar for that year.
The below should return all Tuesdays and Thursdays for a given year.
# Returns all Tuesdays and Thursdays of a given year
from datetime import date
import calendar
year = 2016
c = calendar.TextCalendar(calendar.SUNDAY)
for m in range(1,13):
for i in c.itermonthdays(year,m):
if i != 0: #calendar constructs months with leading zeros (days belongng to the previous month)
day = date(year,m,i)
if day.weekday() == 1 or day.weekday() == 3: #if its Tuesday or Thursday
print "%s-%s-%s" % (year,m,i)
import time
from datetime import timedelta, datetime
first_date = '2021-01-01'
final_date = '2021-12-31'
first_date = datetime.strptime(first_date, '%Y-%m-%d')
last_date = datetime.strptime(final_date, '%Y-%m-%d')
week_day = 'Sunday'
dates = [first_date + timedelta(days=x) for x in range((last_date - first_date).days + 1) if (first_date + timedelta(days=x)).weekday() == time.strptime(week_day, '%A').tm_wday]
It will return all Sunday date of given date range.
Here's a complete generator function that builds on the solution from #sth. It includes the crucial fix that was mentioned in his solution's comments.
You can specify the day of week (using Python's indexing with 0=Monday to 6=Sunday), the starting date, and the number of weeks to enumerate.
def get_all_dates_of_day_of_week_in_year(day_of_week, start_year, start_month,
start_day, max_weeks=None):
'''
Generator function to enumerate all calendar dates for a specific day
of the week during one year. For example, all Wednesdays in 2018 are:
1/3/2018, 1/10/2018, 1/17/2018, 1/24/2018, 1/31/2018, 2/7/2018, etc.
Parameters:
----------
day_of_week : int
The day_of_week should be one of these values: 0=Monday, 1=Tuesday,
2=Wednesday, 3=Thursday, 4=Friday, 5=Saturday, 6=Sunday.
start_year : int
start_month : int
start_day : int
The starting date from which to list out all the dates
max_weeks : int or None
If None, then list out all dates for the rest of the year.
Otherwise, end the list after max_weeks number of weeks.
'''
if day_of_week < 0 or day_of_week > 6:
raise ValueError('day_of_week should be in [0, 6]')
date_iter = date(start_year, start_month, start_day)
# First desired day_of_week
date_iter += timedelta(days=(day_of_week - date_iter.weekday() + 7) % 7)
week = 1
while date_iter.year == start_year:
yield date_iter
date_iter += timedelta(days=7)
if max_weeks is not None:
week += 1
if week > max_weeks:
break
Example usage to get all Wednesdays starting on January 1, 2018, for 10 weeks.
import calendar
day_of_week = 2
max_weeks = 10
for d in get_all_dates_of_day_of_week_in_year (day_of_week, 2018, 1, 1, max_weeks):
print "%s, %d/%d/%d" % (calendar.day_name[d.weekday()], d.year, d.month, d.day)
The above code produces:
Wednesday, 2018/1/3
Wednesday, 2018/1/10
Wednesday, 2018/1/17
Wednesday, 2018/1/24
Wednesday, 2018/1/31
Wednesday, 2018/2/7
Wednesday, 2018/2/14
Wednesday, 2018/2/21
Wednesday, 2018/2/28
Wednesday, 2018/3/7
according to #sth answer I like to give you an alternative without a function
from datetime import date, timedelta,datetime
sunndays = list()
year_var = datetime.now() #get current date
year_var = year_var.year #get only the year
d = date(year_var, 1, 1) #get the 01.01 of the current year = 01.01.2020
#now we have to skip 4 days to get to sunday.
#d.weekday is wednesday so it has a value of 2
d += timedelta(days=6 - d.weekday()) # 01.01.2020 + 4 days (6-2=4)
sunndays.append(str(d.strftime('%d-%m-%Y'))) #you need to catch the first sunday
#here you get every other sundays
while d.year == year_var:
d += timedelta(days=7)
sunndays.append(str(d.strftime('%d-%m-%Y')))
print(sunndays) # only for control
if you want every monday for example
#for 2021 the 01.01 is a friday the value is 4
#we need to skip 3 days 7-4 = 3
d += timedelta(days=7 - d.weekday())
according to #sth answer,it will lost the day when 1st is sunday.This will be better:
d = datetime.date(year, month-1, 28)
for _ in range(5):
d = d + datetime.timedelta(days=-d.weekday(), weeks=1)
if d.month!=month:
break
date.append(d)

Categories

Resources