Output is not how it should be
given_day = datetime.date(2022,10,31)
dates = given_day + datetime.timedelta(days=+1)
for i in range(0 - given_day.weekday(), 7 - given_day.weekday()):
print(given_day.strftime("%A"))
Output should start from Monday and end to Sunday
Output I get is:
Monday
Monday
Monday
Monday
Monday
Monday
Monday
Where i made a mistake :(
I can't make head or tail of your code, but the following works:
given_day = datetime.date(2022,10,31)
for i in range(0 + given_day.weekday(), 7):
print(given_day.strftime("%A"))
given_day += datetime.timedelta(days=1)
Related
input: week_number = 34 (or 2022-34)
expected output:
["2022-08-21","2022-08-22", "2022-08-23","2022-08-24","2022-08-25","2022-08-26","2022-08-27"]
First date should be of Sunday
the last date should be Saturday
work with both leap and non leap year
Try:
import datetime
week_number = 34
out = []
date = datetime.datetime.strptime(f"2022-{week_number}-0", "%Y-%U-%w")
for day in range(7):
out.append((date + datetime.timedelta(days=day)).strftime("%Y-%m-%d"))
print(out)
Prints:
[
"2022-08-21",
"2022-08-22",
"2022-08-23",
"2022-08-24",
"2022-08-25",
"2022-08-26",
"2022-08-27",
]
From the reference:
%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.
If today is November 28, 2021, I want to get last week's Monday and Sunday like:
Monday: 2021-11-15
Sunday: 2021-11-21
I tried like this:
today = datetime.date.today()
idx = (today.weekday() + 1) % 7
self.monday = (today - datetime.timedelta(7 + idx - 1)).strftime('%Y-%m-%d')
self.sunday = (today - datetime.timedelta(7 + idx - 7)).strftime('%Y-%m-%d')
But the output is like this, which is wrong:
Monday: 2021-11-22
Sunday: 2021-11-28
It is correct if today's date is November 29, 2021.
How will I be able to achieve this?
Take today's date and subtract 1 week plus today's weekday 'number':
today = datetime.date.today()
today - datetime.timedelta(days=today.weekday(), weeks=1)
For the Sunday before that, use days=today.weekday() + 1
I’m trying to make a program that tells a shift worker what days he/she is on and what days he/she is off. For example, on 4, off 4.
Here’s my code:
schedule = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
on = 4
off = 4
count = 0
while count < 3:
for x in range(on):
print('on: ', schedule[x])
for y in range(on,len(schedule)):
print('off: ', schedule[y])
count = count + 1
This prints:
on: monday
on: tuesday
on: wednesday
on: thursday
off: friday
off: saturday
off: sunday
X3
What I’m trying to get it to do is:
on: monday
on: tuesday
on: wednesday
on: thursday
off: friday
off: saturday
off: sunday
off: monday
on: tuesday
on: wednesday
on: thursday
on: friday
And so forth.
I think you can simply create a dictionary where you will map Key:Value pair as Days and status and then iterate through it.
for example
schedule = {'monday':"on",'tuesday':"on",'wednesday':"on",'thursday':"on",'friday':"off",'saturday':"off",'sunday':"off"}
and then iterate through the Schedule dict
for day, status in schedule.items():
print(f"{day}:{status}")
I was trying to get the first Friday of the quarter of today's date or any given date. Let's say the date is 12/06/2020, which falls to the 2nd quarter of the year. I should be able to get the first Friday of the 2nd Quarter and that should be April 3rd.
I was only been able to get the quarter of a given date, which you can see in the code below. Been stuck here for a while. Thanks in advance.
quarter = Int((Month(Now) + 2) / 3)
Here's a function that takes a date and returns the first Friday of the quarter:
Function FirstFridayOfTheQuarter(MyDate As Date) As Date
Dim FirstDayOfTheQuarter As Date
FirstDayOfTheQuarter = DateSerial(Year(MyDate), Int((Month(MyDate) - 1) / 3) * 3 + 1, 1)
FirstFridayOfTheQuarter = DateAdd("d", (13 - Weekday(FirstDayOfTheQuarter)) Mod 7, FirstDayOfTheQuarter)
End Function
This function is taking advantage of the Weekday function that returns:
1 for a Sunday
2 for a Monday
3 for a Tuesday
4 for a Wednesday
5 for a Thursday
6 for a Friday
7 for a Saturday
In case you want a non VBA solution:
My formula is:
=CEILING(EOMONTH(DATE(YEAR(A1);ROUNDUP(MONTH(A1)/3;0)+(ROUNDUP(MONTH(A1)/3;0)-1)*2;1);-1)-5;7)+6
I know how to get the last Saturday date using date --date="sat. last week" +"%m%d%Y" However, I am unable to get the last Saturday date for a particular given date.
I am trying to find the date of last saturday date from a given date. For an instance, let us say that the given date is 20140605 (in YYYYMMDD format) or any weekday date from that particular week. This given date could be any weekday date (20140602-20140606) in the following week for that Saturday, I need to derive the Saturday date for the week before this date which would be (in my case)= 20140531. How can I achieve this?
I just barfed a bit on my keyboard, but this seems to work:
D="20140605"; date --date "$D $[($(date --date "$D" +%u) + 1) % 7] days ago" +"%Y%m%d"
I have a small Python snippet for you:
from datetime import datetime, timedelta
import sys
fmt = "%Y%m%d"
daystring = sys.argv[1]
day = datetime.strptime(daystring, fmt)
for d in range(7):
# Go back a few days, but not more than 6.
testdate = day - timedelta(days=d)
# If the test date is a saturday (0 is monday, 6 sunday), output that date.
if testdate.weekday() == 5:
print testdate.strftime(fmt)
Test:
$ python test.py 20140605
20140531